summaryrefslogtreecommitdiffstats
path: root/core/modules/shortcut/src/Form/SetCustomize.php
blob: 20094aa2c304730daeaf62fb890724fa7d8ff442 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php

namespace Drupal\shortcut\Form;

use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;

/**
 * Builds the shortcut set customize form.
 *
 * @internal
 */
class SetCustomize extends EntityForm {

  /**
   * The entity being used by this form.
   *
   * @var \Drupal\shortcut\ShortcutSetInterface
   */
  protected $entity;

  /**
   * {@inheritdoc}
   */
  public function form(array $form, FormStateInterface $form_state) {
    $form = parent::form($form, $form_state);
    $form['shortcuts'] = [
      '#tree' => TRUE,
      '#weight' => -20,
    ];

    $form['shortcuts']['links'] = [
      '#type' => 'table',
      '#header' => [t('Name'), t('Weight'), t('Operations')],
      '#empty' => $this->t('No shortcuts available. <a href=":link">Add a shortcut</a>', [':link' => $this->url('shortcut.link_add', ['shortcut_set' => $this->entity->id()])]),
      '#attributes' => ['id' => 'shortcuts'],
      '#tabledrag' => [
        [
          'action' => 'order',
          'relationship' => 'sibling',
          'group' => 'shortcut-weight',
        ],
      ],
    ];

    foreach ($this->entity->getShortcuts() as $shortcut) {
      $id = $shortcut->id();
      $url = $shortcut->getUrl();
      if (!$url->access()) {
        continue;
      }
      $form['shortcuts']['links'][$id]['#attributes']['class'][] = 'draggable';
      $form['shortcuts']['links'][$id]['name'] = [
        '#type' => 'link',
        '#title' => $shortcut->getTitle(),
      ] + $url->toRenderArray();
      unset($form['shortcuts']['links'][$id]['name']['#access_callback']);
      $form['shortcuts']['links'][$id]['#weight'] = $shortcut->getWeight();
      $form['shortcuts']['links'][$id]['weight'] = [
        '#type' => 'weight',
        '#title' => t('Weight for @title', ['@title' => $shortcut->getTitle()]),
        '#title_display' => 'invisible',
        '#default_value' => $shortcut->getWeight(),
        '#attributes' => ['class' => ['shortcut-weight']],
      ];

      $links['edit'] = [
        'title' => t('Edit'),
        'url' => $shortcut->urlInfo(),
      ];
      $links['delete'] = [
        'title' => t('Delete'),
        'url' => $shortcut->urlInfo('delete-form'),
      ];
      $form['shortcuts']['links'][$id]['operations'] = [
        '#type' => 'operations',
        '#links' => $links,
        '#access' => $url->access(),
      ];
    }
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  protected function actions(array $form, FormStateInterface $form_state) {
    // Only includes a Save action for the entity, no direct Delete button.
    return [
      'submit' => [
        '#type' => 'submit',
        '#value' => t('Save'),
        '#access' => (bool) Element::getVisibleChildren($form['shortcuts']['links']),
        '#submit' => ['::submitForm', '::save'],
      ],
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function save(array $form, FormStateInterface $form_state) {
    foreach ($this->entity->getShortcuts() as $shortcut) {
      $weight = $form_state->getValue(['shortcuts', 'links', $shortcut->id(), 'weight']);
      $shortcut->setWeight($weight);
      $shortcut->save();
    }
    drupal_set_message(t('The shortcut set has been updated.'));
  }

}