comparison core/modules/user/src/RoleForm.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 1fec387a4317
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
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 class RoleForm extends EntityForm {
12
13 /**
14 * {@inheritdoc}
15 */
16 public function form(array $form, FormStateInterface $form_state) {
17 $entity = $this->entity;
18 $form['label'] = [
19 '#type' => 'textfield',
20 '#title' => $this->t('Role name'),
21 '#default_value' => $entity->label(),
22 '#size' => 30,
23 '#required' => TRUE,
24 '#maxlength' => 64,
25 '#description' => $this->t('The name for this role. Example: "Moderator", "Editorial board", "Site architect".'),
26 ];
27 $form['id'] = [
28 '#type' => 'machine_name',
29 '#default_value' => $entity->id(),
30 '#required' => TRUE,
31 '#disabled' => !$entity->isNew(),
32 '#size' => 30,
33 '#maxlength' => 64,
34 '#machine_name' => [
35 'exists' => ['\Drupal\user\Entity\Role', 'load'],
36 ],
37 ];
38 $form['weight'] = [
39 '#type' => 'value',
40 '#value' => $entity->getWeight(),
41 ];
42
43 return parent::form($form, $form_state, $entity);
44 }
45
46 /**
47 * {@inheritdoc}
48 */
49 public function save(array $form, FormStateInterface $form_state) {
50 $entity = $this->entity;
51
52 // Prevent leading and trailing spaces in role names.
53 $entity->set('label', trim($entity->label()));
54 $status = $entity->save();
55
56 $edit_link = $this->entity->link($this->t('Edit'));
57 if ($status == SAVED_UPDATED) {
58 drupal_set_message($this->t('Role %label has been updated.', ['%label' => $entity->label()]));
59 $this->logger('user')->notice('Role %label has been updated.', ['%label' => $entity->label(), 'link' => $edit_link]);
60 }
61 else {
62 drupal_set_message($this->t('Role %label has been added.', ['%label' => $entity->label()]));
63 $this->logger('user')->notice('Role %label has been added.', ['%label' => $entity->label(), 'link' => $edit_link]);
64 }
65 $form_state->setRedirect('entity.user_role.collection');
66 }
67
68 }