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
);
$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->getWeight(),
Alex Pott
committed
);
return parent::form($form, $form_state, $entity);
}
/**
* {@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';