function SetCustomize::form

Same name in other branches
  1. 9 core/modules/shortcut/src/Form/SetCustomize.php \Drupal\shortcut\Form\SetCustomize::form()
  2. 8.9.x core/modules/shortcut/src/Form/SetCustomize.php \Drupal\shortcut\Form\SetCustomize::form()
  3. 11.x core/modules/shortcut/src/Form/SetCustomize.php \Drupal\shortcut\Form\SetCustomize::form()

Overrides EntityForm::form

File

core/modules/shortcut/src/Form/SetCustomize.php, line 27

Class

SetCustomize
Builds the shortcut set customize form.

Namespace

Drupal\shortcut\Form

Code

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' => [
            $this->t('Name'),
            $this->t('Weight'),
            $this->t('Operations'),
        ],
        '#empty' => $this->t('No shortcuts available. <a href=":link">Add a shortcut</a>', [
            ':link' => Url::fromRoute('shortcut.link_add', [
                'shortcut_set' => $this->entity
                    ->id(),
            ])
                ->toString(),
        ]),
        '#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' => $url,
            '#options' => $url->getOptions(),
        ];
        $form['shortcuts']['links'][$id]['#weight'] = $shortcut->getWeight();
        $form['shortcuts']['links'][$id]['weight'] = [
            '#type' => 'weight',
            '#title' => $this->t('Weight for @title', [
                '@title' => $shortcut->getTitle(),
            ]),
            '#title_display' => 'invisible',
            '#default_value' => $shortcut->getWeight(),
            '#attributes' => [
                'class' => [
                    'shortcut-weight',
                ],
            ],
        ];
        $links['edit'] = [
            'title' => $this->t('Edit'),
            'url' => $shortcut->toUrl(),
        ];
        $links['delete'] = [
            'title' => $this->t('Delete'),
            'url' => $shortcut->toUrl('delete-form'),
        ];
        $form['shortcuts']['links'][$id]['operations'] = [
            '#type' => 'operations',
            '#links' => $links,
            '#access' => $url->access(),
        ];
    }
    return $form;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.