You are here

protected function MenuItemExtrasViewModesSettingsForm::buildOverviewForm in Menu Item Extras 8.2

Form constructor to edit an entire menu tree at once.

Shows for one menu the menu links accessible to the current user and relevant operations.

This form constructor can be integrated as a section into another form. It relies on the following keys in $form_state:

  • menu: A menu entity.
  • menu_overview_form_parents: An array containing the parent keys to this form.

Forms integrating this section should call menu_overview_form_submit() from their form submit handler.

1 call to MenuItemExtrasViewModesSettingsForm::buildOverviewForm()
MenuItemExtrasViewModesSettingsForm::form in src/MenuItemExtrasViewModesSettingsForm.php
Gets the actual form array to be built.

File

src/MenuItemExtrasViewModesSettingsForm.php, line 155

Class

MenuItemExtrasViewModesSettingsForm
Base form for menu view modes settings edit forms.

Namespace

Drupal\menu_item_extras

Code

protected function buildOverviewForm(array &$form, FormStateInterface $form_state) {
  if (!$form_state
    ->has('menu_overview_form_parents')) {
    $form_state
      ->set('menu_overview_form_parents', []);
  }
  $tree = $this->menuTree
    ->load($this->entity
    ->id(), new MenuTreeParameters());
  $this
    ->getRequest()->attributes
    ->set('_menu_admin', TRUE);
  $manipulators = [
    [
      'callable' => 'menu.default_tree_manipulators:checkAccess',
    ],
    [
      'callable' => 'menu.default_tree_manipulators:generateIndexAndSort',
    ],
  ];
  $tree = $this->menuTree
    ->transform($tree, $manipulators);
  $this
    ->getRequest()->attributes
    ->set('_menu_admin', FALSE);
  $count = function (array $tree) {
    $sum = function ($carry, MenuLinkTreeElement $item) {
      return $carry + $item
        ->count();
    };
    return array_reduce($tree, $sum);
  };
  $delta = max($count($tree), 50);
  $form['links'] = [
    '#type' => 'table',
    '#theme' => 'table__menu_overview',
    '#header' => [
      $this
        ->t('Menu link'),
      [
        'data' => $this
          ->t('View Mode'),
        'colspan' => 3,
      ],
    ],
    '#attributes' => [
      'entity_id' => 'menu-overview',
    ],
  ];
  $links = $this
    ->buildOverviewTreeForm($tree, $delta);
  foreach (Element::children($links) as $entity_id) {
    if (isset($links[$entity_id]['#item'])) {
      $element = $links[$entity_id];
      $form['links'][$entity_id]['#item'] = $element['#item'];
      $element['parent']['#attributes']['class'][] = 'menu-parent';
      $element['entity_id']['#attributes']['class'][] = 'menu-id';
      $form['links'][$entity_id]['title'] = [
        [
          '#theme' => 'indentation',
          '#size' => $element['#item']->depth - 1,
        ],
        $element['title'],
      ];
      $form['links'][$entity_id]['view_mode'] = $element['view_mode'];
      $form['links'][$entity_id]['entity_id'] = $element['entity_id'];
      $form['links'][$entity_id]['parent'] = $element['parent'];
    }
  }
  return $form;
}