From 61698bd69984ba1b5029698636b516f3ab348efd Mon Sep 17 00:00:00 2001 From: mqanneh <mqanneh@2833163.no-reply.drupal.org> Date: Tue, 19 Dec 2017 11:17:56 +0200 Subject: [PATCH] Issue #2811337 by Johnny vd Laar, woutervu, Sutharsan, vlad.dancer, yannickoo, kevinquillen, Syndz, Gertjan.k, typhonius, mogtofu33, mqanneh: 2+ level menu block not limited to active parent --- src/Plugin/Block/MenuBlock.php | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/Plugin/Block/MenuBlock.php b/src/Plugin/Block/MenuBlock.php index e5790db..f06e8e5 100644 --- a/src/Plugin/Block/MenuBlock.php +++ b/src/Plugin/Block/MenuBlock.php @@ -127,13 +127,36 @@ class MenuBlock extends SystemMenuBlock { if ($depth > 0) { $parameters->setMaxDepth(min($level + $depth - 1, $this->menuTree->maxDepth())); } + + // For menu blocks with start level greater than 1, only show menu items + // from the current active trail. Adjust the root according to the current + // position in the menu in order to determine if we can show the subtree. + // If we're using a fixed parent item, we'll skip this step. + $fixed_parent_menu_link_id = str_replace($menu_name . ':', '', $parent); + if ($level > 1 && !$fixed_parent_menu_link_id) { + if (count($parameters->activeTrail) >= $level) { + // Active trail array is child-first. Reverse it, and pull the new menu + // root based on the parent of the configured start level. + $menu_trail_ids = array_reverse(array_values($parameters->activeTrail)); + $menu_root = $menu_trail_ids[$level - 1]; + $parameters->setRoot($menu_root)->setMinDepth(1); + if ($depth > 0) { + $max_depth = min($level - 1 + $depth - 1, $this->menuTree->maxDepth()); + $parameters->setMaxDepth($max_depth); + } + } + else { + return array(); + } + } + // If expandedParents is empty, the whole menu tree is built. if ($expand) { $parameters->expandedParents = array(); } // When a fixed parent item is set, root the menu tree at the given ID. - if ($menuLinkID = str_replace($menu_name . ':', '', $parent)) { - $parameters->setRoot($menuLinkID); + if ($fixed_parent_menu_link_id) { + $parameters->setRoot($fixed_parent_menu_link_id); // If the starting level is 1, we always want the child links to appear, // but the requested tree may be empty if the tree does not contain the -- 2.7.4