You are here

protected function BigMenuForm::buildOverviewFormWithDepth in Big Menu 8

Same name and namespace in other branches
  1. 2.x src/BigMenuForm.php \Drupal\bigmenu\BigMenuForm::buildOverviewFormWithDepth()

Build a shallow version of the overview form.

Parameters

array $form: The form array.

\Drupal\Core\Form\FormStateInterface $form_state: The form state object.

int $depth: The depth.

string $menu_link: (Optional) The starting menu link id.

Return value

array The form.

1 call to BigMenuForm::buildOverviewFormWithDepth()
BigMenuForm::buildOverviewForm in src/BigMenuForm.php
Overrides Drupal\menu_ui\MenuForm::buildOverviewForm() to limit the depth.

File

src/BigMenuForm.php, line 76

Class

BigMenuForm
Defines class for BigMenuForm.

Namespace

Drupal\bigmenu

Code

protected function buildOverviewFormWithDepth(array &$form, FormStateInterface $form_state, $depth = 1, $menu_link = NULL) {

  // Ensure that menu_overview_form_submit() knows the parents of this form
  // section.
  if (!$form_state
    ->has('menu_overview_form_parents')) {
    $form_state
      ->set('menu_overview_form_parents', []);
  }

  // Use Menu UI adminforms.
  $form['#attached']['library'][] = 'menu_ui/drupal.menu_ui.adminforms';

  // Add a link to go back to the full menu.
  if ($menu_link) {

    /** @var \Drupal\Core\Menu\MenuLinkInterface $parent */
    $breadcrumbs = [];
    $parent = $this->menuLinkManager
      ->createInstance($menu_link);
    while ($parent_id = $parent
      ->getParent()) {
      $parent = $this->menuLinkManager
        ->createInstance($parent_id);
      $breadcrumbs[] = new Link($parent
        ->getTitle(), $this->entity
        ->toUrl('edit-form')
        ->setOption('query', [
        'menu_link' => $parent_id,
      ]));
    }
    $breadcrumbs[] = $this->entity
      ->toLink($this
      ->t('Back to @label top level', [
      '@label' => $this->entity
        ->label(),
    ]), 'edit-form');
    $form['breadcrumb'] = [
      '#theme' => 'breadcrumb',
      '#links' => array_reverse($breadcrumbs),
    ];
  }
  $form['links'] = [
    '#type' => 'table',
    '#theme' => 'table__menu_overview',
    '#header' => [
      $this
        ->t('Menu link'),
      $this
        ->t('Edit children'),
      [
        'data' => $this
          ->t('Enabled'),
        'class' => [
          'checkbox',
        ],
      ],
      $this
        ->t('Weight'),
      [
        'data' => $this
          ->t('Operations'),
        'colspan' => 3,
      ],
    ],
    '#attributes' => [
      'id' => 'menu-overview',
    ],
    '#tabledrag' => [
      [
        'action' => 'match',
        'relationship' => 'parent',
        'group' => 'menu-parent',
        'subgroup' => 'menu-parent',
        'source' => 'menu-id',
        'hidden' => TRUE,
        'limit' => $this->menuTree
          ->maxDepth() - 1,
      ],
      [
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => 'menu-weight',
      ],
    ],
  ];

  // No Links available (Empty menu)
  $form['links']['#empty'] = $this
    ->t('There are no menu links yet. <a href=":url">Add link</a>.', [
    ':url' => $this->entity
      ->toUrl('add-link-form', [
      'query' => [
        'destination' => $this->entity
          ->toUrl('edit-form')
          ->toString(),
      ],
    ])
      ->toString(),
  ]);

  // Get the menu tree if it's not in our property.
  if (empty($this->tree)) {
    $this->tree = $this
      ->getTree($depth, $menu_link);
  }

  // Determine the delta; the number of weights to be made available.
  $count = function (array $tree) {
    $sum = function ($carry, MenuLinkTreeElement $item) {
      return $carry + $item
        ->count();
    };
    return array_reduce($tree, $sum);
  };

  // Tree maximum or 50.
  $delta = max($count($this->tree), 50);
  $links = $this
    ->buildOverviewTreeForm($this->tree, $delta);
  $this
    ->processLinks($form, $links, $menu_link);
  return $form;
}