Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / user / src / RoleForm.php
1 <?php
2
3 namespace Drupal\user;
4
5 use Drupal\Core\Entity\EntityForm;
6 use Drupal\Core\Form\FormStateInterface;
7
8 /**
9  * Form controller for the role entity edit forms.
10  *
11  * @internal
12  */
13 class RoleForm extends EntityForm {
14
15   /**
16    * {@inheritdoc}
17    */
18   public function form(array $form, FormStateInterface $form_state) {
19     $entity = $this->entity;
20     $form['label'] = [
21       '#type' => 'textfield',
22       '#title' => $this->t('Role name'),
23       '#default_value' => $entity->label(),
24       '#size' => 30,
25       '#required' => TRUE,
26       '#maxlength' => 64,
27       '#description' => $this->t('The name for this role. Example: "Moderator", "Editorial board", "Site architect".'),
28     ];
29     $form['id'] = [
30       '#type' => 'machine_name',
31       '#default_value' => $entity->id(),
32       '#required' => TRUE,
33       '#disabled' => !$entity->isNew(),
34       '#size' => 30,
35       '#maxlength' => 64,
36       '#machine_name' => [
37         'exists' => ['\Drupal\user\Entity\Role', 'load'],
38       ],
39     ];
40     $form['weight'] = [
41       '#type' => 'value',
42       '#value' => $entity->getWeight(),
43     ];
44
45     return parent::form($form, $form_state, $entity);
46   }
47
48   /**
49    * {@inheritdoc}
50    */
51   public function save(array $form, FormStateInterface $form_state) {
52     $entity = $this->entity;
53
54     // Prevent leading and trailing spaces in role names.
55     $entity->set('label', trim($entity->label()));
56     $status = $entity->save();
57
58     $edit_link = $this->entity->link($this->t('Edit'));
59     if ($status == SAVED_UPDATED) {
60       $this->messenger()->addStatus($this->t('Role %label has been updated.', ['%label' => $entity->label()]));
61       $this->logger('user')->notice('Role %label has been updated.', ['%label' => $entity->label(), 'link' => $edit_link]);
62     }
63     else {
64       $this->messenger()->addStatus($this->t('Role %label has been added.', ['%label' => $entity->label()]));
65       $this->logger('user')->notice('Role %label has been added.', ['%label' => $entity->label(), 'link' => $edit_link]);
66     }
67     $form_state->setRedirect('entity.user_role.collection');
68   }
69
70 }