Drupal 8 -- custom permissions, drupal custom Permissions

Source: Internet
Author: User
Tags drupal

Drupal 8 -- custom permissions, drupal custom Permissions

In project development, I encountered such a requirement that the title field of the node cannot be edited by the edit content role. The title field is automatically generated when the content type is created. Permissions cannot be directly configured in the drupal8 background, so I need to customize a permission using code.

1. Define a module under/modules/custom. The module name is one_node_title_permission.

 

2. Create three new files: one_node_title_permission.info.yml, one_node_title_permission.module, one_node_title_permission.permissions.yml

Note: The file name must correspond to the module name.

 

3. In the one_node_title_permission.info.yml file, the configuration module information is as follows:

name: One Node Title Permissiondescription: 'Add permission for the title of node.'type: modulecore: 8.xpackage: one

 

4. In the one_node_title_permission.permissions.yml file, configure the permission information to be added, as shown below:

one_node_title_permission permission:    title: 'Edit own value for field field_title'    restrict access: false

 

5. In this way, the basic permissions have been configured. In the/admin/modules directory in the local drupal8 directory, locate the newly added module and check it, as shown in

Then install

 

6. On the/admin/people/permissions page, select a role for permission configuration, select the custom permission, and save

 

7. We added only one permission above, and the function corresponding to the permission has not been added, so I want to add it in one_node_title_permission.module

One_node_title_permission_form_alter function. Add the code I need in this function. Note that this function name is the module name + _ form_alter
<? Php/*** @ file * One Contact US Module. */use Drupal \ Core \ Form \ FormStateInterface;/*** Implements hook_form_alter (). */function one_node_title_permission_form_alter (& $ form, FormStateInterface $ form_state, $ form_id) {$ route_match = \ Drupal: routeMatch (); // get the current path $ user = \ Drupal :: currentUser (); // get the current user role $ access =! $ User-> hasPermission ('one _ node_title_permission permission '); // checks whether this permission is granted. if no false if ($ route_match-> getRouteName () = 'entity. node. edit_form '& $ access) {$ form ['title'] [' # disabled '] = 'Disabled'; // The 'title' field cannot be edited }}

In the above Code, the specific function I want to implement is in the node editing page, users who do not have this permission are not allowed to edit the title field.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.