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