function UserPermissionsForm::buildForm

Same name in other branches
  1. 8.9.x core/modules/user/src/Form/UserPermissionsForm.php \Drupal\user\Form\UserPermissionsForm::buildForm()
  2. 10 core/modules/user/src/Form/UserPermissionsForm.php \Drupal\user\Form\UserPermissionsForm::buildForm()
  3. 11.x core/modules/user/src/Form/UserPermissionsForm.php \Drupal\user\Form\UserPermissionsForm::buildForm()

Overrides FormInterface::buildForm

3 calls to UserPermissionsForm::buildForm()
EntityPermissionsForm::buildForm in core/modules/user/src/Form/EntityPermissionsForm.php
Builds the user permissions administration form for a bundle.
UserPermissionsModuleSpecificForm::buildForm in core/modules/user/src/Form/UserPermissionsModuleSpecificForm.php
Builds the user permissions administration form for a specific module(s).
UserPermissionsRoleSpecificForm::buildForm in core/modules/user/src/Form/UserPermissionsRoleSpecificForm.php
Builds the user permissions administration form for a specific role.
3 methods override UserPermissionsForm::buildForm()
EntityPermissionsForm::buildForm in core/modules/user/src/Form/EntityPermissionsForm.php
Builds the user permissions administration form for a bundle.
UserPermissionsModuleSpecificForm::buildForm in core/modules/user/src/Form/UserPermissionsModuleSpecificForm.php
Builds the user permissions administration form for a specific module(s).
UserPermissionsRoleSpecificForm::buildForm in core/modules/user/src/Form/UserPermissionsRoleSpecificForm.php
Builds the user permissions administration form for a specific role.

File

core/modules/user/src/Form/UserPermissionsForm.php, line 120

Class

UserPermissionsForm
Provides the user permissions administration form.

Namespace

Drupal\user\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
    $role_names = [];
    $role_permissions = [];
    $admin_roles = [];
    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'] = [
        '#type' => 'value',
        '#value' => $role_names,
    ];
    // Render role/permission overview:
    $hide_descriptions = system_admin_compact_mode();
    $form['system_compact_link'] = [
        '#id' => FALSE,
        '#type' => 'system_compact_link',
    ];
    $form['permissions'] = [
        '#type' => 'table',
        '#header' => [
            $this->t('Permission'),
        ],
        '#id' => 'permissions',
        '#attributes' => [
            'class' => [
                'permissions',
                'js-permissions',
            ],
        ],
        '#sticky' => TRUE,
    ];
    foreach ($role_names as $name) {
        $form['permissions']['#header'][] = [
            'data' => $name,
            'class' => [
                'checkbox',
            ],
        ];
    }
    foreach ($this->permissionsByProvider() as $provider => $permissions) {
        // Module name.
        $form['permissions'][$provider] = [
            [
                '#wrapper_attributes' => [
                    'colspan' => count($role_names) + 1,
                    'class' => [
                        'module',
                    ],
                    'id' => 'module-' . $provider,
                ],
                '#markup' => $this->moduleHandler
                    ->getName($provider),
            ],
        ];
        foreach ($permissions as $perm => $perm_item) {
            // Fill in default values for the permission.
            $perm_item += [
                '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'] = [
                '#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' => [
                    '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] = [
                    '#title' => $name . ': ' . $perm_item['title'],
                    '#title_display' => 'invisible',
                    '#wrapper_attributes' => [
                        'class' => [
                            'checkbox',
                        ],
                    ],
                    '#type' => 'checkbox',
                    '#default_value' => in_array($perm, $role_permissions[$rid]) ? 1 : 0,
                    '#attributes' => [
                        'class' => [
                            'rid-' . $rid,
                            'js-rid-' . $rid,
                        ],
                    ],
                    '#parents' => [
                        $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'] = [
        '#type' => 'actions',
    ];
    $form['actions']['submit'] = [
        '#type' => 'submit',
        '#value' => $this->t('Save permissions'),
        '#button_type' => 'primary',
    ];
    $form['#attached']['library'][] = 'user/drupal.user.permissions';
    return $form;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.