public function MenuViewBuilder::build in Twig Tweak 3.x
Same name and namespace in other branches
- 3.1.x src/View/MenuViewBuilder.php \Drupal\twig_tweak\View\MenuViewBuilder::build()
Returns the render array for a menu.
Parameters
string $menu_name: The name of the menu.
int $level: (optional) Initial menu level.
int $depth: (optional) Maximum number of menu levels to display.
bool $expand: (optional) Expand all menu links.
Return value
array A render array for the menu.
See also
\Drupal\system\Plugin\Block\SystemMenuBlock::build()
File
- src/
View/ MenuViewBuilder.php, line 43
Class
- MenuViewBuilder
- Menu view builder.
Namespace
Drupal\twig_tweak\ViewCode
public function build(string $menu_name, int $level = 1, int $depth = 0, bool $expand = FALSE) : array {
$parameters = $this->menuLinkTree
->getCurrentRouteMenuTreeParameters($menu_name);
// Adjust the menu tree parameters based on the block's configuration.
$parameters
->setMinDepth($level);
// When the depth is configured to zero, there is no depth limit. When depth
// is non-zero, it indicates the number of levels that must be displayed.
// Hence this is a relative depth that we must convert to an actual
// (absolute) depth, that may never exceed the maximum depth.
if ($depth > 0) {
$parameters
->setMaxDepth(min($level + $depth - 1, $this->menuLinkTree
->maxDepth()));
}
// If expandedParents is empty, the whole menu tree is built.
if ($expand) {
$parameters->expandedParents = [];
}
$tree = $this->menuLinkTree
->load($menu_name, $parameters);
$manipulators = [
[
'callable' => 'menu.default_tree_manipulators:checkAccess',
],
[
'callable' => 'menu.default_tree_manipulators:generateIndexAndSort',
],
];
$tree = $this->menuLinkTree
->transform($tree, $manipulators);
$build = $this->menuLinkTree
->build($tree);
if (!isset($build['#cache']['keys'])) {
$build['#cache']['keys'] = [
'twig_tweak_menu',
$menu_name,
'[level]=' . $level,
'[depth]=' . $depth,
'[expand]=' . (int) $expand,
];
}
return $build;
}