diff --git a/core/modules/system/src/Plugin/Block/SystemMenuBlock.php b/core/modules/system/src/Plugin/Block/SystemMenuBlock.php index 83a2f85..1816c42 100644 --- a/core/modules/system/src/Plugin/Block/SystemMenuBlock.php +++ b/core/modules/system/src/Plugin/Block/SystemMenuBlock.php @@ -101,6 +101,13 @@ class SystemMenuBlock extends BlockBase implements ContainerFactoryPluginInterfa '#required' => TRUE, ); + $form['menu_levels']['follow'] = array( + '#type' => 'checkbox', + '#title' => $this->t('Make the starting level follow the active menu item.'), + '#default_value' => $config['follow'], + '#description' => $this->t('If the active menu item is deeper than the level specified above, the starting level will follow the active menu item. Otherwise, the starting level of the tree will remain fixed.') + ); + $options[0] = $this->t('Unlimited'); $form['menu_levels']['depth'] = array( @@ -131,6 +138,7 @@ class SystemMenuBlock extends BlockBase implements ContainerFactoryPluginInterfa public function blockSubmit($form, FormStateInterface $form_state) { $this->configuration['level'] = $form_state->getValue('level'); $this->configuration['depth'] = $form_state->getValue('depth'); + $this->configuration['follow'] = $form_state->getValue('follow'); } /** @@ -152,6 +160,15 @@ class SystemMenuBlock extends BlockBase implements ContainerFactoryPluginInterfa $parameters->setMaxDepth(min($level + $depth - 1, $this->menuTree->maxDepth())); } + if ($level > 1 && $this->configuration['follow']) { + if (count($parameters->activeTrail) >= $level) { + $menu_root = array_values($parameters->activeTrail)[count($parameters->activeTrail) - $level]; + $parameters->setRoot($menu_root)->setMinDepth(1); + } else { + return; + } + } + $tree = $this->menuTree->load($menu_name, $parameters); $manipulators = array( array('callable' => 'menu.default_tree_manipulators:checkAccess'), @@ -168,6 +185,7 @@ class SystemMenuBlock extends BlockBase implements ContainerFactoryPluginInterfa return [ 'level' => 1, 'depth' => 0, + 'follow' => 0, ]; }