public UserPermissionsForm::buildForm(array $form, FormStateInterface $form_state)
Form constructor.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Return value
array The form structure.
Overrides FormInterface::buildForm
File
- core/modules/user/src/Form/UserPermissionsForm.php, line 85
Class
- UserPermissionsForm
- Provides the user permissions administration form.
Namespace
Drupal\user\Form
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | public function buildForm( array $form , FormStateInterface $form_state ) { $role_names = array (); $role_permissions = array (); $admin_roles = array (); foreach ( $this ->getRoles() as $role_name => $role ) { // Retrieve role names for columns. $role_names [ $role_name ] = $role ->label(); // Fetch permissions for the roles. $role_permissions [ $role_name ] = $role ->getPermissions(); $admin_roles [ $role_name ] = $role ->isAdmin(); } // Store $role_names for use when saving the data. $form [ 'role_names' ] = array ( '#type' => 'value' , '#value' => $role_names , ); // Render role/permission overview: $hide_descriptions = system_admin_compact_mode(); $form [ 'system_compact_link' ] = array ( '#id' => FALSE, '#type' => 'system_compact_link' , ); $form [ 'permissions' ] = array ( '#type' => 'table' , '#header' => array ( $this ->t( 'Permission' )), '#id' => 'permissions' , '#attributes' => [ 'class' => [ 'permissions' , 'js-permissions' ]], '#sticky' => TRUE, ); foreach ( $role_names as $name ) { $form [ 'permissions' ][ '#header' ][] = array ( 'data' => $name , 'class' => array ( 'checkbox' ), ); } $permissions = $this ->permissionHandler->getPermissions(); $permissions_by_provider = array (); foreach ( $permissions as $permission_name => $permission ) { $permissions_by_provider [ $permission [ 'provider' ]][ $permission_name ] = $permission ; } foreach ( $permissions_by_provider as $provider => $permissions ) { // Module name. $form [ 'permissions' ][ $provider ] = array ( array ( '#wrapper_attributes' => array ( 'colspan' => count ( $role_names ) + 1, 'class' => array ( 'module' ), 'id' => 'module-' . $provider , ), '#markup' => $this ->moduleHandler->getName( $provider ), )); foreach ( $permissions as $perm => $perm_item ) { // Fill in default values for the permission. $perm_item += array ( 'description' => '' , 'restrict access' => FALSE, 'warning' => ! empty ( $perm_item [ 'restrict access' ]) ? $this ->t( 'Warning: Give to trusted roles only; this permission has security implications.' ) : '' , ); $form [ 'permissions' ][ $perm ][ 'description' ] = array ( '#type' => 'inline_template' , '#template' => '<div class="permission"><span class="title">{{ title }}</span>{% if description or warning %}<div class="description">{% if warning %}<em class="permission-warning">{{ warning }}</em> {% endif %}{{ description }}</div>{% endif %}</div>' , '#context' => array ( 'title' => $perm_item [ 'title' ], ), ); // Show the permission description. if (! $hide_descriptions ) { $form [ 'permissions' ][ $perm ][ 'description' ][ '#context' ][ 'description' ] = $perm_item [ 'description' ]; $form [ 'permissions' ][ $perm ][ 'description' ][ '#context' ][ 'warning' ] = $perm_item [ 'warning' ]; } foreach ( $role_names as $rid => $name ) { $form [ 'permissions' ][ $perm ][ $rid ] = array ( '#title' => $name . ': ' . $perm_item [ 'title' ], '#title_display' => 'invisible' , '#wrapper_attributes' => array ( 'class' => array ( 'checkbox' ), ), '#type' => 'checkbox' , '#default_value' => in_array( $perm , $role_permissions [ $rid ]) ? 1 : 0, '#attributes' => array ( 'class' => array ( 'rid-' . $rid , 'js-rid-' . $rid )), '#parents' => array ( $rid , $perm ), ); // Show a column of disabled but checked checkboxes. if ( $admin_roles [ $rid ]) { $form [ 'permissions' ][ $perm ][ $rid ][ '#disabled' ] = TRUE; $form [ 'permissions' ][ $perm ][ $rid ][ '#default_value' ] = TRUE; } } } } $form [ 'actions' ] = array ( '#type' => 'actions' ); $form [ 'actions' ][ 'submit' ] = array ( '#type' => 'submit' , '#value' => $this ->t( 'Save permissions' ), '#button_type' => 'primary' , ); $form [ '#attached' ][ 'library' ][] = 'user/drupal.user.permissions' ; return $form ; } |
Please login to continue.