MenuForm::buildOverviewForm

protected MenuForm::buildOverviewForm(array &$form, FormStateInterface $form_state)

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.

File

core/modules/menu_ui/src/MenuForm.php, line 215

Class

MenuForm
Base form for menu edit forms.

Namespace

Drupal\menu_ui

Code

protected function buildOverviewForm(array &$form, FormStateInterface $form_state) {
  // 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', []);
  }

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

  $tree = $this->menuTree->load($this->entity->id(), new MenuTreeParameters());

  // We indicate that a menu administrator is running the menu access check.
  $this->getRequest()->attributes->set('_menu_admin', TRUE);
  $manipulators = array(
    array('callable' => 'menu.default_tree_manipulators:checkAccess'),
    array('callable' => 'menu.default_tree_manipulators:generateIndexAndSort'),
  );
  $tree = $this->menuTree->transform($tree, $manipulators);
  $this->getRequest()->attributes->set('_menu_admin', FALSE);

  // 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);
  };
  $delta = max($count($tree), 50);

  $form['links'] = array(
    '#type' => 'table',
    '#theme' => 'table__menu_overview',
    '#header' => array(
      $this->t('Menu link'),
      array(
        'data' => $this->t('Enabled'),
        'class' => array('checkbox'),
      ),
      $this->t('Weight'),
      array(
        'data' => $this->t('Operations'),
        'colspan' => 3,
      ),
    ),
    '#attributes' => array(
      'id' => 'menu-overview',
    ),
    '#tabledrag' => array(
      array(
        'action' => 'match',
        'relationship' => 'parent',
        'group' => 'menu-parent',
        'subgroup' => 'menu-parent',
        'source' => 'menu-id',
        'hidden' => TRUE,
        'limit' => \Drupal::menuTree()->maxDepth() - 1,
      ),
      array(
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => 'menu-weight',
      ),
    ),
  );

  $form['links']['#empty'] = $this->t('There are no menu links yet. <a href=":url">Add link</a>.', [
    ':url' => $this->url('entity.menu.add_link_form', ['menu' => $this->entity->id()], [
      'query' => ['destination' => $this->entity->url('edit-form')],
    ]),
  ]);
  $links = $this->buildOverviewTreeForm($tree, $delta);
  foreach (Element::children($links) as $id) {
    if (isset($links[$id]['#item'])) {
      $element = $links[$id];

      $form['links'][$id]['#item'] = $element['#item'];

      // TableDrag: Mark the table row as draggable.
      $form['links'][$id]['#attributes'] = $element['#attributes'];
      $form['links'][$id]['#attributes']['class'][] = 'draggable';

      // TableDrag: Sort the table row according to its existing/configured weight.
      $form['links'][$id]['#weight'] = $element['#item']->link->getWeight();

      // Add special classes to be used for tabledrag.js.
      $element['parent']['#attributes']['class'] = array('menu-parent');
      $element['weight']['#attributes']['class'] = array('menu-weight');
      $element['id']['#attributes']['class'] = array('menu-id');

      $form['links'][$id]['title'] = array(
        array(
          '#theme' => 'indentation',
          '#size' => $element['#item']->depth - 1,
        ),
        $element['title'],
      );
      $form['links'][$id]['enabled'] = $element['enabled'];
      $form['links'][$id]['enabled']['#wrapper_attributes']['class'] = array('checkbox', 'menu-enabled');

      $form['links'][$id]['weight'] = $element['weight'];

      // Operations (dropbutton) column.
      $form['links'][$id]['operations'] = $element['operations'];

      $form['links'][$id]['id'] = $element['id'];
      $form['links'][$id]['parent'] = $element['parent'];
    }
  }

  return $form;
}
doc_Drupal
2016-10-29 09:26:16
Comments
Leave a Comment

Please login to continue.