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