Browse Source

Issue #2945085 by smk-ka: Add option to expand menu items

merge-requests/4/head
smk-ka 7 years ago committed by Chi
parent
commit
ce15ad15ba
  1. 9
      src/TwigExtension.php

9
src/TwigExtension.php

@ -206,11 +206,13 @@ class TwigExtension extends \Twig_Extension {
* (optional) Initial menu level. * (optional) Initial menu level.
* @param int $depth * @param int $depth
* (optional) Maximum number of menu levels to display. * (optional) Maximum number of menu levels to display.
* @param bool $expand
* (optional) Expand all menu links.
* *
* @return array * @return array
* A render array for the menu. * A render array for the menu.
*/ */
public function drupalMenu($menu_name, $level = 1, $depth = 0) { public function drupalMenu($menu_name, $level = 1, $depth = 0, $expand = FALSE) {
/** @var \Drupal\Core\Menu\MenuLinkTreeInterface $menu_tree */ /** @var \Drupal\Core\Menu\MenuLinkTreeInterface $menu_tree */
$menu_tree = \Drupal::service('menu.link_tree'); $menu_tree = \Drupal::service('menu.link_tree');
$parameters = $menu_tree->getCurrentRouteMenuTreeParameters($menu_name); $parameters = $menu_tree->getCurrentRouteMenuTreeParameters($menu_name);
@ -225,6 +227,11 @@ class TwigExtension extends \Twig_Extension {
$parameters->setMaxDepth(min($level + $depth - 1, $menu_tree->maxDepth())); $parameters->setMaxDepth(min($level + $depth - 1, $menu_tree->maxDepth()));
} }
// If expandedParents is empty, the whole menu tree is built.
if ($expand) {
$parameters->expandedParents = [];
}
$tree = $menu_tree->load($menu_name, $parameters); $tree = $menu_tree->load($menu_name, $parameters);
$manipulators = [ $manipulators = [
['callable' => 'menu.default_tree_manipulators:checkAccess'], ['callable' => 'menu.default_tree_manipulators:checkAccess'],

Loading…
Cancel
Save