function RoleSettingsForm::buildForm

Same name in other branches
  1. 9 core/modules/user/src/Form/RoleSettingsForm.php \Drupal\user\Form\RoleSettingsForm::buildForm()
  2. 11.x core/modules/user/src/Form/RoleSettingsForm.php \Drupal\user\Form\RoleSettingsForm::buildForm()

Overrides FormInterface::buildForm

File

core/modules/user/src/Form/RoleSettingsForm.php, line 52

Class

RoleSettingsForm
Configure administrator role settings for this site.

Namespace

Drupal\user\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
    // Administrative role option.
    $form['admin_role'] = [
        '#type' => 'details',
        '#title' => $this->t('Administrator role'),
        '#open' => TRUE,
    ];
    // Do not allow users to set the anonymous or authenticated user roles as
    // the administrator role.
    $roles = $this->roleStorage
        ->loadMultiple();
    unset($roles[RoleInterface::ANONYMOUS_ID]);
    unset($roles[RoleInterface::AUTHENTICATED_ID]);
    $roles = array_map(fn(RoleInterface $role) => $role->label(), $roles);
    $admin_roles = $this->roleStorage
        ->getQuery()
        ->condition('is_admin', TRUE)
        ->execute();
    $default_value = reset($admin_roles);
    $form['admin_role']['user_admin_role'] = [
        '#type' => 'select',
        '#title' => $this->t('Administrator role'),
        '#empty_value' => '',
        '#default_value' => $default_value,
        '#options' => $roles,
        '#description' => $this->t('This role will be automatically granted all permissions.'),
        // Don't allow to select a single admin role in case multiple roles got
        // marked as admin role already.
'#access' => count($admin_roles) <= 1,
    ];
    $form['actions'] = [
        '#type' => 'actions',
    ];
    $form['actions']['submit'] = [
        '#type' => 'submit',
        '#value' => $this->t('Save configuration'),
        '#button_type' => 'primary',
    ];
    return $form;
}

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