You are here

protected function MenuForm::buildLinkElement in Colossal Menu 8

Same name and namespace in other branches
  1. 2.x src/Form/MenuForm.php \Drupal\colossal_menu\Form\MenuForm::buildLinkElement()

Build an array of link elements.

Parameters

array $elements: An array of form elements to be filled.

\Drupal\Core\Menu\MenuLinkTreeElement $item: Menu Link Tree element.

int $depth: The current depth.

1 call to MenuForm::buildLinkElement()
MenuForm::buildOverviewForm in src/Form/MenuForm.php
Form constructor to edit an entire menu tree at once.

File

src/Form/MenuForm.php, line 243

Class

MenuForm
Settings form for menus.

Namespace

Drupal\colossal_menu\Form

Code

protected function buildLinkElement(array &$elements, MenuLinkTreeElement $item, $depth = 0) {

  /** @var \Drupal\colossal_menu\Entity\Link $link */
  $link = $item->link;
  $id = $link
    ->id();
  $elements[$id] = [
    '#weight' => $link
      ->getWeight(),
  ];
  if ($link
    ->get('parent')
    ->access('edit') && $link
    ->get('weight')
    ->access('edit')) {
    $elements[$id]['#attributes']['class'][] = 'draggable';
  }
  $text = [];
  if (!$link
    ->isExternal() && $link
    ->getRouteName() == '<none>') {
    $text = [
      '#plain_text' => $link
        ->getTitle(),
    ];
  }
  else {
    $text = Link::fromTextAndUrl($link
      ->getTitle(), $link
      ->getUrlObject())
      ->toRenderable();
  }
  $elements[$id]['indent'] = [
    [
      '#theme' => 'indentation',
      '#size' => $depth,
    ],
    [
      $text,
    ],
  ];
  if ($link
    ->get('enabled')
    ->access('edit')) {
    $elements[$id]['enabled'] = [
      '#type' => 'checkbox',
      '#default_value' => $link
        ->isEnabled(),
      '#title' => $this
        ->t('Enabled'),
      '#title_display' => 'invisible',
    ];
  }
  else {
    $elements[$id]['enabled'] = [
      '#markup' => $link
        ->isEnabled() ? $this
        ->t('Enabled') : $this
        ->t('Disabled'),
    ];
  }
  $elements[$id]['weight'] = [
    '#type' => 'weight',
    '#delta' => 50,
    '#default_value' => $link
      ->getWeight(),
    '#title' => $this
      ->t('Weight for @title', [
      '@title' => $link
        ->getTitle(),
    ]),
    '#title_display' => 'invisible',
    '#attributes' => [
      'class' => [
        'link-weight',
      ],
    ],
    '#access' => $link
      ->get('weight')
      ->access('edit'),
  ];
  $operations = [];
  if ($link
    ->access('update')) {
    $operations['edit'] = [
      'title' => $this
        ->t('Edit'),
      'url' => $link
        ->getEditRoute(),
      'query' => $this
        ->getDestinationArray(),
    ];
  }
  if ($link
    ->access('delete')) {
    $operations['delete'] = [
      'title' => $this
        ->t('Delete'),
      'url' => $link
        ->getDeleteRoute(),
      'query' => $this
        ->getDestinationArray(),
    ];
  }
  $elements[$id]['operations'] = [
    '#type' => 'operations',
    '#links' => $operations,
  ];
  $elements[$id]['id'] = [
    '#type' => 'hidden',
    '#default_value' => $id,
    '#attributes' => [
      'class' => [
        'link-id',
      ],
    ],
  ];
  $elements[$id]['parent'] = [
    '#type' => 'hidden',
    '#default_value' => $link
      ->getParent() ? $link
      ->getParent()
      ->id() : 0,
    '#attributes' => [
      'class' => [
        'link-parent',
      ],
    ],
    '#access' => $link
      ->get('parent')
      ->access('edit'),
  ];
  if (!empty($item->subtree)) {
    $subdepth = $depth + 1;
    foreach ($item->subtree as $subitem) {
      $this
        ->buildLinkElement($elements, $subitem, $subdepth);
    }
  }
}