class RoleForm
Same name and namespace in other branches
- 11.x core/modules/user/src/RoleForm.php \Drupal\user\RoleForm
- 10 core/modules/user/src/RoleForm.php \Drupal\user\RoleForm
- 8.9.x core/modules/user/src/RoleForm.php \Drupal\user\RoleForm
Form controller for the role entity edit forms.
@internal
Hierarchy
- class \Drupal\Core\Form\FormBase implements \Drupal\Core\Form\FormInterface, \Drupal\Core\DependencyInjection\ContainerInjectionInterface uses \Drupal\Core\DependencyInjection\DependencySerializationTrait, \Drupal\Core\Logger\LoggerChannelTrait, \Drupal\Core\Messenger\MessengerTrait, \Drupal\Core\Routing\RedirectDestinationTrait, \Drupal\Core\StringTranslation\StringTranslationTrait
- class \Drupal\Core\Entity\EntityForm implements \Drupal\Core\Entity\EntityFormInterface extends \Drupal\Core\Form\FormBase
- class \Drupal\user\RoleForm extends \Drupal\Core\Entity\EntityForm
- class \Drupal\Core\Entity\EntityForm implements \Drupal\Core\Entity\EntityFormInterface extends \Drupal\Core\Form\FormBase
Expanded class hierarchy of RoleForm
File
-
core/
modules/ user/ src/ RoleForm.php, line 13
Namespace
Drupal\userView source
class RoleForm extends EntityForm {
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
$entity = $this->entity;
$form['label'] = [
'#type' => 'textfield',
'#title' => $this->t('Role name'),
'#default_value' => $entity->label(),
'#size' => 30,
'#required' => TRUE,
'#maxlength' => 64,
'#description' => $this->t('The name for this role. Example: "Moderator", "Editorial board", "Site architect".'),
];
$form['id'] = [
'#type' => 'machine_name',
'#default_value' => $entity->id(),
'#required' => TRUE,
'#disabled' => !$entity->isNew(),
'#size' => 30,
'#maxlength' => 64,
'#machine_name' => [
'exists' => [
'\\Drupal\\user\\Entity\\Role',
'load',
],
],
];
$form['weight'] = [
'#type' => 'value',
'#value' => $entity->getWeight(),
];
return parent::form($form, $form_state, $entity);
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
$entity = $this->entity;
// Prevent leading and trailing spaces in role names.
$entity->set('label', trim($entity->label()));
$status = $entity->save();
$edit_link = $this->entity
->toLink($this->t('Edit'), 'edit-form')
->toString();
if ($status == SAVED_UPDATED) {
$this->messenger()
->addStatus($this->t('Role %label has been updated.', [
'%label' => $entity->label(),
]));
$this->logger('user')
->notice('Role %label has been updated.', [
'%label' => $entity->label(),
'link' => $edit_link,
]);
}
else {
$this->messenger()
->addStatus($this->t('Role %label has been added.', [
'%label' => $entity->label(),
]));
$this->logger('user')
->notice('Role %label has been added.', [
'%label' => $entity->label(),
'link' => $edit_link,
]);
}
$form_state->setRedirect('entity.user_role.collection');
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.