Skip to content
SetCustomize.php 3.2 KiB
Newer Older

/**
 * Builds the shortcut set customize form.
   * The entity being used by this form.
   * @var \Drupal\shortcut\ShortcutSetInterface
  public function form(array $form, FormStateInterface $form_state) {
    $form = parent::form($form, $form_state);
      '#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) {
      $url = $shortcut->getUrl();
      if (!$url->access()) {
        continue;
      }
      $form['shortcuts']['links'][$id]['#attributes']['class'][] = 'draggable';
      $form['shortcuts']['links'][$id]['name'] = [
        '#type' => 'link',
        '#title' => $shortcut->getTitle(),
      unset($form['shortcuts']['links'][$id]['name']['#access_callback']);
      $form['shortcuts']['links'][$id]['#weight'] = $shortcut->getWeight();
      $form['shortcuts']['links'][$id]['weight'] = [
        '#title' => t('Weight for @title', ['@title' => $shortcut->getTitle()]),
        '#default_value' => $shortcut->getWeight(),
        '#attributes' => ['class' => ['shortcut-weight']],
      ];
        'url' => $shortcut->urlInfo('delete-form'),
      ];
      $form['shortcuts']['links'][$id]['operations'] = [
        '#type' => 'operations',
        '#links' => $links,
  protected function actions(array $form, FormStateInterface $form_state) {
    // Only includes a Save action for the entity, no direct Delete button.
        '#access' => (bool) Element::getVisibleChildren($form['shortcuts']['links']),
        '#submit' => ['::submitForm', '::save'],
      ],
    ];
  public function save(array $form, FormStateInterface $form_state) {
    foreach ($this->entity->getShortcuts() as $shortcut) {
      $weight = $form_state->getValue(['shortcuts', 'links', $shortcut->id(), 'weight']);
    $this->messenger()->addStatus($this->t('The shortcut set has been updated.'));