RoleSettingsForm.php in Drupal 10
File
core/modules/user/src/Form/RoleSettingsForm.php
View source
<?php
namespace Drupal\user\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\user\RoleInterface;
use Drupal\user\RoleStorageInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class RoleSettingsForm extends FormBase {
protected $roleStorage;
public function __construct(RoleStorageInterface $role_storage) {
$this->roleStorage = $role_storage;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager')
->getStorage('user_role'));
}
public function getFormId() {
return 'role_settings';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['admin_role'] = [
'#type' => 'details',
'#title' => $this
->t('Administrator role'),
'#open' => TRUE,
];
$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.'),
'#access' => count($admin_roles) <= 1,
];
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Save configuration'),
'#button_type' => 'primary',
];
return $form;
}
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();
}
}
}
}