class RoleSettingsForm

Same name in other branches
  1. 10 core/modules/user/src/Form/RoleSettingsForm.php \Drupal\user\Form\RoleSettingsForm
  2. 11.x core/modules/user/src/Form/RoleSettingsForm.php \Drupal\user\Form\RoleSettingsForm

Configure administrator role settings for this site.

Hierarchy

Expanded class hierarchy of RoleSettingsForm

1 string reference to 'RoleSettingsForm'
user.routing.yml in core/modules/user/user.routing.yml
core/modules/user/user.routing.yml

File

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

Namespace

Drupal\user\Form
View source
class RoleSettingsForm extends FormBase {
    
    /**
     * The role storage used when changing the admin role.
     *
     * @var \Drupal\user\RoleStorageInterface
     */
    protected $roleStorage;
    
    /**
     * Constructs a \Drupal\user\Form\RoleSettingsForm object.
     *
     * @param \Drupal\user\RoleStorageInterface $role_storage
     *   The role storage.
     */
    public function __construct(RoleStorageInterface $role_storage) {
        $this->roleStorage = $role_storage;
    }
    
    /**
     * {@inheritdoc}
     */
    public static function create(ContainerInterface $container) {
        return new static($container->get('entity_type.manager')
            ->getStorage('user_role'));
    }
    
    /**
     * {@inheritdoc}
     */
    public function getFormId() {
        return 'role_settings';
    }
    
    /**
     * {@inheritdoc}
     */
    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 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;
    }
    
    /**
     * {@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();
            }
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 3
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 3
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route.
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 73
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 17
MessengerTrait::messenger public function Gets the messenger. 17
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a &#039;destination&#039; URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
RoleSettingsForm::$roleStorage protected property The role storage used when changing the admin role.
RoleSettingsForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
RoleSettingsForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
RoleSettingsForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
RoleSettingsForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
RoleSettingsForm::__construct public function Constructs a \Drupal\user\Form\RoleSettingsForm object.
StringTranslationTrait::$stringTranslation protected property The string translation service. 3
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.

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