diff --git a/core/lib/Drupal/Core/Menu/MenuTreeParameters.php b/core/lib/Drupal/Core/Menu/MenuTreeParameters.php index 2ea493d..472f15d 100644 --- a/core/lib/Drupal/Core/Menu/MenuTreeParameters.php +++ b/core/lib/Drupal/Core/Menu/MenuTreeParameters.php @@ -138,6 +138,16 @@ public function addExpandedParents(array $parents) { } /** + * Empty the expanded parents variable, to expand all items of the menu tree. + * + * @return $this + */ + public function emptyExpandedParents() { + $this->expandedParents = array(); + return $this; + } + + /** * Sets the active trail IDs used to set the inActiveTrail property. * * @param string[] $active_trail diff --git a/core/modules/menu_ui/src/Tests/MenuTest.php b/core/modules/menu_ui/src/Tests/MenuTest.php index d88911a..3311ac6 100644 --- a/core/modules/menu_ui/src/Tests/MenuTest.php +++ b/core/modules/menu_ui/src/Tests/MenuTest.php @@ -150,6 +150,28 @@ function testMenu() { } /** + * Test the "expand all items" feature. + */ + public function testExpandAllItems() { + $this->drupalLogin($this->adminUser); + $menu = $this->addCustomMenu(); + $node = $this->drupalCreateNode(array('type' => 'article')); + $item1 = $this->addMenuLink('', '/node/' . $node->id(), $menu->id(), FALSE); + $item2 = $this->addMenuLink($item1->getPluginId(), '/node/' . $node->id(), $menu->id(), FALSE); + $item3 = $this->addMenuLink($item1->getPluginId(), '/node/' . $node->id(), $menu->id()); + $block_id = $this->blockPlacements[$menu->id()]; + $this->drupalGet('admin/structure/block/manage/' . $block_id); + $this->drupalPostForm(NULL, [ + 'settings[depth]' => 2, + 'settings[level]' => 1, + 'settings[expand_all_items]' => 1, + ], t('Save block')); + $this->verifyMenuLink($item1, $node); + $this->verifyMenuLink($item2, $node); + $this->verifyMenuLink($item3, $node); + } + + /** * Adds a custom menu using CRUD functions. */ function addCustomMenuCRUD() { diff --git a/core/modules/system/config/schema/system.schema.yml b/core/modules/system/config/schema/system.schema.yml index e34d375..0e58508 100644 --- a/core/modules/system/config/schema/system.schema.yml +++ b/core/modules/system/config/schema/system.schema.yml @@ -338,6 +338,9 @@ block.settings.system_menu_block:*: depth: type: integer label: 'Maximum number of levels' + expand_all_items: + type: boolean + label: 'Expand all items' block.settings.local_tasks_block: type: block_settings diff --git a/core/modules/system/src/Plugin/Block/SystemMenuBlock.php b/core/modules/system/src/Plugin/Block/SystemMenuBlock.php index 83a2f85..464a95e 100644 --- a/core/modules/system/src/Plugin/Block/SystemMenuBlock.php +++ b/core/modules/system/src/Plugin/Block/SystemMenuBlock.php @@ -112,6 +112,13 @@ public function blockForm($form, FormStateInterface $form_state) { '#required' => TRUE, ); + $form['expand_all_items'] = array( + '#type' => 'checkbox', + '#title' => $this->t('Expand all items in this tree'), + '#default_value' => !empty($config['expand_all_items']), + '#description' => $this->t('Expand all items of this menu tree to show all items including the active trail.'), + ); + return $form; } @@ -131,6 +138,7 @@ public static function processMenuLevelParents(&$element, FormStateInterface $fo public function blockSubmit($form, FormStateInterface $form_state) { $this->configuration['level'] = $form_state->getValue('level'); $this->configuration['depth'] = $form_state->getValue('depth'); + $this->configuration['expand_all_items'] = $form_state->getValue('expand_all_items'); } /** @@ -151,6 +159,9 @@ public function build() { if ($depth > 0) { $parameters->setMaxDepth(min($level + $depth - 1, $this->menuTree->maxDepth())); } + if (!empty($this->configuration['expand_all_items'])) { + $parameters->emptyExpandedParents(); + } $tree = $this->menuTree->load($menu_name, $parameters); $manipulators = array( @@ -168,6 +179,7 @@ public function defaultConfiguration() { return [ 'level' => 1, 'depth' => 0, + 'expand_all_items' => FALSE, ]; }