You are here

public function RoleSettingsForm::buildForm in Drupal 9

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/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 = user_role_names(TRUE);
  unset($roles[RoleInterface::AUTHENTICATED_ID]);
  $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 assigned new permissions whenever a module is enabled. Changing this setting will not affect existing 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;
}