function ToolbarController::preRenderGetRenderedSubtrees

Same name in other branches
  1. 9 core/modules/toolbar/src/Controller/ToolbarController.php \Drupal\toolbar\Controller\ToolbarController::preRenderGetRenderedSubtrees()
  2. 8.9.x core/modules/toolbar/src/Controller/ToolbarController.php \Drupal\toolbar\Controller\ToolbarController::preRenderGetRenderedSubtrees()
  3. 10 core/modules/toolbar/src/Controller/ToolbarController.php \Drupal\toolbar\Controller\ToolbarController::preRenderGetRenderedSubtrees()

#pre_render callback for toolbar_get_rendered_subtrees().

@internal

File

core/modules/toolbar/src/Controller/ToolbarController.php, line 105

Class

ToolbarController
Defines a controller for the toolbar module.

Namespace

Drupal\toolbar\Controller

Code

public static function preRenderGetRenderedSubtrees(array $data) {
    $menu_tree = \Drupal::service('toolbar.menu_tree');
    $renderer = \Drupal::service('renderer');
    // Load the administration menu. The first level is the "Administration"
    // link. In order to load the children of that link and the subsequent two
    // levels, start at the second level and end at the fourth.
    $parameters = new MenuTreeParameters();
    $parameters->setMinDepth(2)
        ->setMaxDepth(4)
        ->onlyEnabledLinks();
    // @todo Make the menu configurable in https://www.drupal.org/node/1869638.
    $tree = $menu_tree->load('admin', $parameters);
    $manipulators = [
        [
            'callable' => 'menu.default_tree_manipulators:checkAccess',
        ],
        [
            'callable' => 'menu.default_tree_manipulators:generateIndexAndSort',
        ],
        [
            'callable' => 'toolbar_menu_navigation_links',
        ],
    ];
    $tree = $menu_tree->transform($tree, $manipulators);
    $subtrees = [];
    // Calculated the combined cacheability of all subtrees.
    $cacheability = CacheableMetadata::createFromRenderArray($data);
    foreach ($tree as $element) {
        
        /** @var \Drupal\Core\Menu\MenuLinkInterface $link */
        $link = $element->link;
        if ($element->subtree) {
            $subtree = $menu_tree->build($element->subtree);
            $output = $renderer->executeInRenderContext(new RenderContext(), function () use ($renderer, $subtree) {
                return $renderer->render($subtree);
            });
            $cacheability = $cacheability->merge(CacheableMetadata::createFromRenderArray($subtree));
        }
        else {
            $output = '';
        }
        // Many routes have dots as route name, while some special ones like
        // <front> have <> characters in them.
        $url = $link->getUrlObject();
        $id = str_replace([
            '.',
            '<',
            '>',
        ], [
            '-',
            '',
            '',
        ], $url->isRouted() ? $url->getRouteName() : $url->getUri());
        $subtrees[$id] = $output;
    }
    // Store the subtrees, along with the cacheability metadata.
    $cacheability->applyTo($data);
    $data['#subtrees'] = $subtrees;
    return $data;
}

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