Newer
Older
Angie Byron
committed
<?php
/**
* @file
Alex Pott
committed
* Contains \Drupal\shortcut\ShortcutSetForm.
Angie Byron
committed
*/
namespace Drupal\shortcut;
Alex Pott
committed
use Drupal\Core\Entity\EntityForm;
Dries Buytaert
committed
use Drupal\Core\Form\FormStateInterface;
Angie Byron
committed
/**
* Form controller for the shortcut set entity edit forms.
*/
Alex Pott
committed
class ShortcutSetForm extends EntityForm {
Angie Byron
committed
/**
Alex Pott
committed
* {@inheritdoc}
Angie Byron
committed
*/
Dries Buytaert
committed
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
Angie Byron
committed
$entity = $this->entity;
Angie Byron
committed
$form['label'] = array(
'#type' => 'textfield',
'#title' => t('Set name'),
'#description' => t('The new set is created by copying items from your default shortcut set.'),
'#required' => TRUE,
'#default_value' => $entity->label(),
);
$form['id'] = array(
'#type' => 'machine_name',
'#machine_name' => array(
'exists' => '\Drupal\shortcut\Entity\ShortcutSet::load',
Angie Byron
committed
'source' => array('label'),
'replace_pattern' => '[^a-z0-9-]+',
'replace' => '-',
),
'#default_value' => $entity->id(),
'#disabled' => !$entity->isNew(),
// This id could be used for menu name.
'#maxlength' => 23,
);
$form['actions']['submit']['#value'] = t('Create new set');
return $form;
}
/**
Alex Pott
committed
* {@inheritdoc}
Angie Byron
committed
*/
Dries Buytaert
committed
public function validate(array $form, FormStateInterface $form_state) {
Angie Byron
committed
parent::validate($form, $form_state);
$entity = $this->entity;
Angie Byron
committed
// Check to prevent a duplicate title.
Alex Pott
committed
if ($form_state->getValue('label') != $entity->label() && shortcut_set_title_exists($form_state->getValue('label'))) {
$form_state->setErrorByName('label', $this->t('The shortcut set %name already exists. Choose another name.', array('%name' => $form_state->getValue('label'))));
Angie Byron
committed
}
}
/**
Alex Pott
committed
* {@inheritdoc}
Angie Byron
committed
*/
Dries Buytaert
committed
public function save(array $form, FormStateInterface $form_state) {
$entity = $this->entity;
$is_new = !$entity->getOriginalId();
Angie Byron
committed
$entity->save();
if ($is_new) {
drupal_set_message(t('The %set_name shortcut set has been created. You can edit it from this page.', array('%set_name' => $entity->label())));
}
else {
drupal_set_message(t('Updated set name to %set-name.', array('%set-name' => $entity->label())));
}
Alex Pott
committed
$form_state->setRedirectUrl($this->entity->urlInfo('customize-form'));