3 namespace Drupal\shortcut\Form;
5 use Drupal\Core\Entity\EntityForm;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\Core\Render\Element;
10 * Builds the shortcut set customize form.
12 class SetCustomize extends EntityForm {
15 * The entity being used by this form.
17 * @var \Drupal\shortcut\ShortcutSetInterface
24 public function form(array $form, FormStateInterface $form_state) {
25 $form = parent::form($form, $form_state);
26 $form['shortcuts'] = [
31 $form['shortcuts']['links'] = [
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'],
39 'relationship' => 'sibling',
40 'group' => 'shortcut-weight',
45 foreach ($this->entity->getShortcuts() as $shortcut) {
46 $id = $shortcut->id();
47 $url = $shortcut->getUrl();
48 if (!$url->access()) {
51 $form['shortcuts']['links'][$id]['#attributes']['class'][] = 'draggable';
52 $form['shortcuts']['links'][$id]['name'] = [
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'] = [
60 '#title' => t('Weight for @title', ['@title' => $shortcut->getTitle()]),
61 '#title_display' => 'invisible',
62 '#default_value' => $shortcut->getWeight(),
63 '#attributes' => ['class' => ['shortcut-weight']],
68 'url' => $shortcut->urlInfo(),
71 'title' => t('Delete'),
72 'url' => $shortcut->urlInfo('delete-form'),
74 $form['shortcuts']['links'][$id]['operations'] = [
75 '#type' => 'operations',
77 '#access' => $url->access(),
86 protected function actions(array $form, FormStateInterface $form_state) {
87 // Only includes a Save action for the entity, no direct Delete button.
91 '#value' => t('Save'),
92 '#access' => (bool) Element::getVisibleChildren($form['shortcuts']['links']),
93 '#submit' => ['::submitForm', '::save'],
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);
107 drupal_set_message(t('The shortcut set has been updated.'));