2a27cb7029997c2afffd0c66bed9538a60be393b
[yaffs-website] / web / core / modules / shortcut / src / ShortcutSetForm.php
1 <?php
2
3 namespace Drupal\shortcut;
4
5 use Drupal\Core\Entity\BundleEntityFormBase;
6 use Drupal\Core\Form\FormStateInterface;
7
8 /**
9  * Form handler for the shortcut set entity edit forms.
10  *
11  * @internal
12  */
13 class ShortcutSetForm extends BundleEntityFormBase {
14
15   /**
16    * {@inheritdoc}
17    */
18   public function form(array $form, FormStateInterface $form_state) {
19     $form = parent::form($form, $form_state);
20
21     $entity = $this->entity;
22     $form['label'] = [
23       '#type' => 'textfield',
24       '#title' => t('Set name'),
25       '#description' => t('The new set is created by copying items from your default shortcut set.'),
26       '#required' => TRUE,
27       '#default_value' => $entity->label(),
28     ];
29     $form['id'] = [
30       '#type' => 'machine_name',
31       '#machine_name' => [
32         'exists' => '\Drupal\shortcut\Entity\ShortcutSet::load',
33         'source' => ['label'],
34         'replace_pattern' => '[^a-z0-9-]+',
35         'replace' => '-',
36       ],
37       '#default_value' => $entity->id(),
38       // This id could be used for menu name.
39       '#maxlength' => 23,
40     ];
41
42     $form['actions']['submit']['#value'] = t('Create new set');
43
44     return $this->protectBundleIdElement($form);
45   }
46
47   /**
48    * {@inheritdoc}
49    */
50   public function save(array $form, FormStateInterface $form_state) {
51     $entity = $this->entity;
52     $is_new = !$entity->getOriginalId();
53     $entity->save();
54
55     if ($is_new) {
56       $this->messenger()->addStatus($this->t('The %set_name shortcut set has been created. You can edit it from this page.', ['%set_name' => $entity->label()]));
57     }
58     else {
59       $this->messenger()->addStatus($this->t('Updated set name to %set-name.', ['%set-name' => $entity->label()]));
60     }
61     $form_state->setRedirectUrl($this->entity->urlInfo('customize-form'));
62   }
63
64 }