diff -u b/core/modules/user/src/AdministratorRoleSettingsForm.php b/core/modules/user/src/AdministratorRoleSettingsForm.php --- b/core/modules/user/src/AdministratorRoleSettingsForm.php +++ b/core/modules/user/src/AdministratorRoleSettingsForm.php @@ -1,124 +1,117 @@ moduleHandler = $module_handler; - $this->roleStorage = $role_storage; - } - - /** - * {@inheritdoc} - */ - public static function create(ContainerInterface $container) - { - return new static( - $container->get('config.factory'), - $container->get('module_handler'), - $container->get('entity.manager')->getStorage('user_role') - ); - } - - /** - * {@inheritdoc} - */ - public function getFormId() - { - return 'administrator_role_settings'; - } - - /** - * {@inheritdoc} - */ - protected function getEditableConfigNames() - { - return [ - 'system.site', - 'user.mail', - 'user.settings', - ]; - } - - /** - * {@inheritdoc} - */ - public function buildForm(array $form, FormStateInterface $form_state) - { - $form = parent::buildForm($form, $form_state); - $config = $this->config('user.settings'); - $mail_config = $this->config('user.mail'); - $site_config = $this->config('system.site'); - // 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, - ]; - return parent::buildForm($form, $form_state); - } +class AdministratorRoleSettingsForm extends ConfigFormBase { - /** - * {@inheritdoc} - */ - public function submitForm(array &$form, FormStateInterface $form_state) - { - $saved_role = $form_state->getValue('user_admin_role'); - if ($form_state->hasValue('user_admin_role')) { - $admin_roles = $this->roleStorage->getQuery() - ->condition('is_admin', true) - ->execute(); - - foreach ($admin_roles as $rid) { - $this->roleStorage->load($rid)->setIsAdmin(false)->save(); - } - - $new_admin_role = $form_state->getValue('user_admin_role'); - if ($new_admin_role) { - $this->roleStorage->load($new_admin_role)->setIsAdmin(true)->save(); - } - } - parent::submitForm($form, $form_state); + /** + * The role storage used when changing the admin role. + * + * @var \Drupal\user\RoleStorageInterface + */ + protected $roleStorage; + + /** + * Constructs a \Drupal\user\AdministratorRoleSettingsForm object. + * + * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory + * The factory for configuration objects. + * @param \Drupal\user\RoleStorageInterface $role_storage + * The role storage. + */ + public function __construct(ConfigFactoryInterface $config_factory, RoleStorageInterface $role_storage) { + parent::__construct($config_factory); + $this->roleStorage = $role_storage; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('config.factory'), + $container->get('entity.manager')->getStorage('user_role') + ); + } + + /** + * {@inheritdoc} + */ + public function getFormId() { + return 'administrator_role_settings'; + } + + /** + * {@inheritdoc} + */ + protected function getEditableConfigNames() { + return [ + 'system.site', + 'user.mail', + 'user.settings', + ]; + } + + /** + * {@inheritdoc} + */ + public function buildForm(array $form, FormStateInterface $form_state) { + $form = parent::buildForm($form, $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, + ]; + + return parent::buildForm($form, $form_state); + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + if ($form_state->hasValue('user_admin_role')) { + $admin_roles = $this->roleStorage->getQuery() + ->condition('is_admin', TRUE) + ->execute(); + foreach ($admin_roles as $rid) { + $this->roleStorage->load($rid)->setIsAdmin(FALSE)->save(); + } + $new_admin_role = $form_state->getValue('user_admin_role'); + if ($new_admin_role) { + $this->roleStorage->load($new_admin_role)->setIsAdmin(TRUE)->save(); + } } + parent::submitForm($form, $form_state); + } }