function MenuForm::buildOverviewTreeForm

Same name in other branches
  1. 9 core/modules/menu_ui/src/MenuForm.php \Drupal\menu_ui\MenuForm::buildOverviewTreeForm()
  2. 8.9.x core/modules/menu_ui/src/MenuForm.php \Drupal\menu_ui\MenuForm::buildOverviewTreeForm()
  3. 11.x core/modules/menu_ui/src/MenuForm.php \Drupal\menu_ui\MenuForm::buildOverviewTreeForm()

Recursive helper function for buildOverviewForm().

Parameters

\Drupal\Core\Menu\MenuLinkTreeElement[] $tree: The tree retrieved by \Drupal\Core\Menu\MenuLinkTreeInterface::load().

int $delta: The default number of menu items used in the menu weight selector is 50.

Return value

array The overview tree form.

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

File

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

Class

MenuForm
Base form for menu edit forms.

Namespace

Drupal\menu_ui

Code

protected function buildOverviewTreeForm($tree, $delta) {
    $form =& $this->overviewTreeForm;
    $tree_access_cacheability = new CacheableMetadata();
    foreach ($tree as $element) {
        $tree_access_cacheability = $tree_access_cacheability->merge(CacheableMetadata::createFromObject($element->access));
        // Only render accessible links.
        if (!$element->access
            ->isAllowed()) {
            continue;
        }
        
        /** @var \Drupal\Core\Menu\MenuLinkInterface $link */
        $link = $element->link;
        if ($link) {
            $id = 'menu_plugin_id:' . $link->getPluginId();
            $form[$id]['#item'] = $element;
            $form[$id]['#attributes'] = $link->isEnabled() ? [
                'class' => [
                    'menu-enabled',
                ],
            ] : [
                'class' => [
                    'menu-disabled',
                ],
            ];
            $form[$id]['title'] = Link::fromTextAndUrl($link->getTitle(), $link->getUrlObject())
                ->toRenderable();
            if (!$link->isEnabled()) {
                $form[$id]['title']['#suffix'] = ' (' . $this->t('disabled') . ')';
            }
            elseif ($id === 'menu_plugin_id:user.logout') {
                $form[$id]['title']['#suffix'] = ' (' . $this->t('<q>Log in</q> for anonymous users') . ')';
            }
            elseif (($url = $link->getUrlObject()) && $url->isRouted() && $url->getRouteName() == 'user.page') {
                $form[$id]['title']['#suffix'] = ' (' . $this->t('logged in users only') . ')';
            }
            $form[$id]['enabled'] = [
                '#type' => 'checkbox',
                '#title' => $this->t('Enable @title menu link', [
                    '@title' => $link->getTitle(),
                ]),
                '#title_display' => 'invisible',
                '#default_value' => $link->isEnabled(),
            ];
            $form[$id]['weight'] = [
                '#type' => 'weight',
                '#delta' => $delta,
                '#default_value' => $link->getWeight(),
                '#title' => $this->t('Weight for @title', [
                    '@title' => $link->getTitle(),
                ]),
                '#title_display' => 'invisible',
            ];
            $form[$id]['id'] = [
                '#type' => 'hidden',
                '#value' => $link->getPluginId(),
            ];
            $form[$id]['parent'] = [
                '#type' => 'hidden',
                '#default_value' => $link->getParent(),
            ];
            $operations = $link->getOperations();
            if ($element->depth < $this->menuTree
                ->maxDepth()) {
                $add_link_url = Url::fromRoute('entity.menu.add_link_form', [
                    'menu' => $this->entity
                        ->id(),
                ], [
                    'query' => [
                        'parent' => $link->getPluginId(),
                    ],
                ]);
                $operations += [
                    'add-child' => [
                        'title' => $this->t('Add child'),
                        'weight' => 20,
                        'url' => $add_link_url,
                    ],
                ];
                uasort($operations, [
                    SortArray::class,
                    'sortByWeightElement',
                ]);
            }
            foreach ($operations as $key => $operation) {
                if (!isset($operations[$key]['query'])) {
                    // Bring the user back to the menu overview.
                    $operations[$key]['query'] = $this->getDestinationArray();
                }
            }
            $form[$id]['operations'] = [
                '#type' => 'operations',
                '#links' => $operations,
            ];
        }
        if ($element->subtree) {
            $this->buildOverviewTreeForm($element->subtree, $delta);
        }
    }
    $tree_access_cacheability->merge(CacheableMetadata::createFromRenderArray($form))
        ->applyTo($form);
    return $form;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.