From 91915d256923b747a0b495040171f5af05ff594f Mon Sep 17 00:00:00 2001 From: bcanini <canini.16@osu.edu> Date: Fri, 18 Jan 2019 12:00:39 -0500 Subject: [PATCH] patch - menu_block: allowing 2nd level menu_block not limited to active parent --- composer.json | 3 ++- vendor/composer/installed.json | 3 ++- .../menu_block/src/Plugin/Block/MenuBlock.php | 27 +++++++++++++++++-- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index 9487be5030..40dfe121f1 100644 --- a/composer.json +++ b/composer.json @@ -244,7 +244,8 @@ "2962965": "https://www.drupal.org/files/issues/2018-09-01/block-permissions-2962965-4.patch" }, "drupal/menu_block": { - "2809699": "https://www.drupal.org/files/issues/2018-10-26/menu_block-label_configuration-2809699-82.patch" + "2809699": "https://www.drupal.org/files/issues/2018-10-26/menu_block-label_configuration-2809699-82.patch", + "2811337": "https://www.drupal.org/files/issues/menu_block-2_level_menu_block_not_limited_to_active_parent-2811337-58.patch" }, "drupal/entity_embed": { "2881745": "https://www.drupal.org/files/issues/2018-12-04/2881745-22.patch" diff --git a/vendor/composer/installed.json b/vendor/composer/installed.json index 6d36966ae3..1fb1be93a1 100644 --- a/vendor/composer/installed.json +++ b/vendor/composer/installed.json @@ -4636,7 +4636,8 @@ } }, "patches_applied": { - "2809699": "https://www.drupal.org/files/issues/2018-10-26/menu_block-label_configuration-2809699-82.patch" + "2809699": "https://www.drupal.org/files/issues/2018-10-26/menu_block-label_configuration-2809699-82.patch", + "2811337": "https://www.drupal.org/files/issues/menu_block-2_level_menu_block_not_limited_to_active_parent-2811337-58.patch" } }, "installation-source": "dist", diff --git a/web/modules/menu_block/src/Plugin/Block/MenuBlock.php b/web/modules/menu_block/src/Plugin/Block/MenuBlock.php index d60627de3c..39f8569a62 100644 --- a/web/modules/menu_block/src/Plugin/Block/MenuBlock.php +++ b/web/modules/menu_block/src/Plugin/Block/MenuBlock.php @@ -209,13 +209,36 @@ public function build() { 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 -- GitLab