diff --git a/core/modules/system/src/Plugin/Block/SystemMenuBlock.php b/core/modules/system/src/Plugin/Block/SystemMenuBlock.php index 83a2f85..00d58e0 100644 --- a/core/modules/system/src/Plugin/Block/SystemMenuBlock.php +++ b/core/modules/system/src/Plugin/Block/SystemMenuBlock.php @@ -140,16 +140,31 @@ public function build() { $menu_name = $this->getDerivativeId(); $parameters = $this->menuTree->getCurrentRouteMenuTreeParameters($menu_name); - // Adjust the menu tree parameters based on the block's configuration. + // Get the the block's configuration. $level = $this->configuration['level']; $depth = $this->configuration['depth']; - $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->menuTree->maxDepth())); + + // Get the active trail. + $active_trail = $this->menuActiveTrail->getActiveTrailIds($menu_name); + + if(count($active_trail) < $level) { + // Page is above the menu's starting level. Just hide the menu. + $parameters->setMinDepth($level); + + } else { + // Get the root of the menu. + $menu_root = array_values($active_trail)[count($active_trail) - $level]; + // Set the root of the tree. + $parameters->setRoot($menu_root); + // Set min depth to 1 since the menu tree now starts at the correct root. + $parameters->setMinDepth(1); + // When the depth is configured to zero, there is no depth limit. When depth + // is non-zero, it indicates the number of levels that can be displayed. + // With the correct root set, max depth is simply $depth. Make sure it + // does not exceed the maxiumum depth allowed. + if ($depth > 0) { + $parameters->setMaxDepth(min($depth, $this->menuTree->maxDepth())); + } } $tree = $this->menuTree->load($menu_name, $parameters);