Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / shortcut / src / Form / SetCustomize.php
1 <?php
2
3 namespace Drupal\shortcut\Form;
4
5 use Drupal\Core\Entity\EntityForm;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\Core\Render\Element;
8
9 /**
10  * Builds the shortcut set customize form.
11  *
12  * @internal
13  */
14 class SetCustomize extends EntityForm {
15
16   /**
17    * The entity being used by this form.
18    *
19    * @var \Drupal\shortcut\ShortcutSetInterface
20    */
21   protected $entity;
22
23   /**
24    * {@inheritdoc}
25    */
26   public function form(array $form, FormStateInterface $form_state) {
27     $form = parent::form($form, $form_state);
28     $form['shortcuts'] = [
29       '#tree' => TRUE,
30       '#weight' => -20,
31     ];
32
33     $form['shortcuts']['links'] = [
34       '#type' => 'table',
35       '#header' => [t('Name'), t('Weight'), t('Operations')],
36       '#empty' => $this->t('No shortcuts available. <a href=":link">Add a shortcut</a>', [':link' => $this->url('shortcut.link_add', ['shortcut_set' => $this->entity->id()])]),
37       '#attributes' => ['id' => 'shortcuts'],
38       '#tabledrag' => [
39         [
40           'action' => 'order',
41           'relationship' => 'sibling',
42           'group' => 'shortcut-weight',
43         ],
44       ],
45     ];
46
47     foreach ($this->entity->getShortcuts() as $shortcut) {
48       $id = $shortcut->id();
49       $url = $shortcut->getUrl();
50       if (!$url->access()) {
51         continue;
52       }
53       $form['shortcuts']['links'][$id]['#attributes']['class'][] = 'draggable';
54       $form['shortcuts']['links'][$id]['name'] = [
55         '#type' => 'link',
56         '#title' => $shortcut->getTitle(),
57       ] + $url->toRenderArray();
58       unset($form['shortcuts']['links'][$id]['name']['#access_callback']);
59       $form['shortcuts']['links'][$id]['#weight'] = $shortcut->getWeight();
60       $form['shortcuts']['links'][$id]['weight'] = [
61         '#type' => 'weight',
62         '#title' => t('Weight for @title', ['@title' => $shortcut->getTitle()]),
63         '#title_display' => 'invisible',
64         '#default_value' => $shortcut->getWeight(),
65         '#attributes' => ['class' => ['shortcut-weight']],
66       ];
67
68       $links['edit'] = [
69         'title' => t('Edit'),
70         'url' => $shortcut->urlInfo(),
71       ];
72       $links['delete'] = [
73         'title' => t('Delete'),
74         'url' => $shortcut->urlInfo('delete-form'),
75       ];
76       $form['shortcuts']['links'][$id]['operations'] = [
77         '#type' => 'operations',
78         '#links' => $links,
79         '#access' => $url->access(),
80       ];
81     }
82     return $form;
83   }
84
85   /**
86    * {@inheritdoc}
87    */
88   protected function actions(array $form, FormStateInterface $form_state) {
89     // Only includes a Save action for the entity, no direct Delete button.
90     return [
91       'submit' => [
92         '#type' => 'submit',
93         '#value' => t('Save'),
94         '#access' => (bool) Element::getVisibleChildren($form['shortcuts']['links']),
95         '#submit' => ['::submitForm', '::save'],
96       ],
97     ];
98   }
99
100   /**
101    * {@inheritdoc}
102    */
103   public function save(array $form, FormStateInterface $form_state) {
104     foreach ($this->entity->getShortcuts() as $shortcut) {
105       $weight = $form_state->getValue(['shortcuts', 'links', $shortcut->id(), 'weight']);
106       $shortcut->setWeight($weight);
107       $shortcut->save();
108     }
109     $this->messenger()->addStatus($this->t('The shortcut set has been updated.'));
110   }
111
112 }