You are here

public function SetCustomize::form in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/shortcut/src/Form/SetCustomize.php \Drupal\shortcut\Form\SetCustomize::form()

Gets the actual form array to be built.

Overrides EntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

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
      ->toRenderArray();
    unset($form['shortcuts']['links'][$id]['name']['#access_callback']);
    $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;
}