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;
Dries Buytaert
committed
use Drupal\Core\Form\FormStateInterface;
Alex Pott
committed
/**
* Form controller for the role entity edit forms.
*/
Alex Pott
committed
class RoleForm extends EntityForm {
Alex Pott
committed
/**
* {@inheritdoc}
*/
Dries Buytaert
committed
public function form(array $form, FormStateInterface $form_state) {
Alex Pott
committed
$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' => ['\Drupal\user\Entity\Role', 'load'],
Alex Pott
committed
),
);
$form['weight'] = array(
'#type' => 'value',
'#value' => $entity->getWeight(),
Alex Pott
committed
);
return parent::form($form, $form_state, $entity);
}
/**
* {@inheritdoc}
*/
Dries Buytaert
committed
public function save(array $form, FormStateInterface $form_state) {
Alex Pott
committed
$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 = $this->entity->link($this->t('Edit'));
Alex Pott
committed
if ($status == SAVED_UPDATED) {
drupal_set_message($this->t('Role %label has been updated.', array('%label' => $entity->label())));
$this->logger('user')->notice('Role %label has been updated.', array('%label' => $entity->label(), 'link' => $edit_link));
Alex Pott
committed
}
else {
drupal_set_message($this->t('Role %label has been added.', array('%label' => $entity->label())));
$this->logger('user')->notice('Role %label has been added.', array('%label' => $entity->label(), 'link' => $edit_link));
Alex Pott
committed
}
Alex Pott
committed
$form_state->setRedirect('entity.user_role.collection');