Newer
Older
Angie Byron
committed
<?php
namespace Drupal\shortcut;
use Drupal\Core\Entity\BundleEntityFormBase;
Dries Buytaert
committed
use Drupal\Core\Form\FormStateInterface;
Angie Byron
committed
/**
* Form handler for the shortcut set entity edit forms.
*
* @internal
Angie Byron
committed
*/
class ShortcutSetForm extends BundleEntityFormBase {
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;
$form['label'] = [
Angie Byron
committed
'#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'] = [
Angie Byron
committed
'#type' => 'machine_name',
'#machine_name' => [
'exists' => '\Drupal\shortcut\Entity\ShortcutSet::load',
'source' => ['label'],
Angie Byron
committed
'replace_pattern' => '[^a-z0-9-]+',
'replace' => '-',
],
Angie Byron
committed
'#default_value' => $entity->id(),
// This id could be used for menu name.
'#maxlength' => 23,
];
Angie Byron
committed
$form['actions']['submit']['#value'] = t('Create new set');
return $this->protectBundleIdElement($form);
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.', ['%set_name' => $entity->label()]));
Angie Byron
committed
}
else {
drupal_set_message(t('Updated set name to %set-name.', ['%set-name' => $entity->label()]));
Angie Byron
committed
}
Alex Pott
committed
$form_state->setRedirectUrl($this->entity->urlInfo('customize-form'));