Newer
Older
Alex Pott
committed
<?php
/**
* @file
Alex Pott
committed
* Contains \Drupal\user\RoleForm.
Alex Pott
committed
*/
namespace Drupal\user;
use Drupal\Core\Entity\EntityInterface;
Alex Pott
committed
use Drupal\Core\Entity\EntityForm;
Alex Pott
committed
/**
* Form controller for the role entity edit forms.
*/
Alex Pott
committed
class RoleForm extends EntityForm {
Alex Pott
committed
/**
* {@inheritdoc}
*/
public function form(array $form, array &$form_state) {
$entity = $this->entity;
$form['label'] = array(
'#type' => 'textfield',
'#title' => $this->t('Role name'),
Alex Pott
committed
'#default_value' => $entity->label(),
'#size' => 30,
'#required' => TRUE,
'#maxlength' => 64,
'#description' => $this->t('The name for this role. Example: "Moderator", "Editorial board", "Site architect".'),
Alex Pott
committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
);
$form['id'] = array(
'#type' => 'machine_name',
'#default_value' => $entity->id(),
'#required' => TRUE,
'#disabled' => !$entity->isNew(),
'#size' => 30,
'#maxlength' => 64,
'#machine_name' => array(
'exists' => 'user_role_load',
),
);
$form['weight'] = array(
'#type' => 'value',
'#value' => $entity->get('weight'),
);
return parent::form($form, $form_state, $entity);
}
/**
* {@inheritdoc}
*/
protected function actions(array $form, array &$form_state) {
$actions = parent::actions($form, $form_state);
// Disable delete of new and built-in roles.
$actions['delete']['#access'] = !$this->entity->isNew() && !in_array($this->entity->id(), array(DRUPAL_ANONYMOUS_RID, DRUPAL_AUTHENTICATED_RID));
return $actions;
}
/**
* {@inheritdoc}
*/
public function save(array $form, array &$form_state) {
$entity = $this->entity;
// Prevent leading and trailing spaces in role names.
$entity->set('label', trim($entity->label()));
Alex Pott
committed
$status = $entity->save();
Angie Byron
committed
$edit_link = \Drupal::linkGenerator()->generateFromUrl($this->t('Edit'), $this->entity->urlInfo());
Alex Pott
committed
if ($status == SAVED_UPDATED) {
drupal_set_message($this->t('Role %label has been updated.', array('%label' => $entity->label())));
Alex Pott
committed
watchdog('user', 'Role %label has been updated.', array('%label' => $entity->label()), WATCHDOG_NOTICE, $edit_link);
Alex Pott
committed
}
else {
drupal_set_message($this->t('Role %label has been added.', array('%label' => $entity->label())));
Alex Pott
committed
watchdog('user', 'Role %label has been added.', array('%label' => $entity->label()), WATCHDOG_NOTICE, $edit_link);
Alex Pott
committed
}
Angie Byron
committed
$form_state['redirect_route']['route_name'] = 'user.role_list';