4

I'd like to have a menu show as expanded regardless of whether it is checked or not in the UI (or always checked when a new menu item is created by a user)

I have been looking at hook_menu_links_discovered_alter

https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Menu%21menu.api.php/function/hook_menu_links_discovered_alter/8.1.x

expanded: (optional) If set to TRUE, and if a menu link is provided for this menu item (as a result of other properties), then the menu link is always expanded, equivalent to its 'always expanded' checkbox being set in the UI.

How could I implement this?

  • Menu Block will solve this in 2 seconds. It will force all children to be expanded for every menu you configure for it. – Kevin Jun 24 '17 at 19:05
  • Good chance we'll have this as an adjustable option in D8.5 drupal.org/node/2594425 – leymannx Oct 17 '17 at 17:35
3

If you are placing the menu in a block for display, check out the Menu Block module https://www.drupal.org/project/menu_block it has an option to expand all menu links.

  • This is the easiest way to do it, and Menu Block gives you more options as to how to display the menu and make theming much easier. – Kevin Jun 24 '17 at 1:41
0

If this is for a custom module:

1) Make a file called [modulename].links.menu.yml

2) add information about the link to the file, for example:

[modulename].menulink1:
title: 'View 1'
parent: main
menu_name: main
route_name: [modulename].route1
expanded: TRUE
weight: 2

[modulename].menulink_child1:
title: 'View 2'
description: 'View the second'
parent: [modulename].menulink1
route_name: [modulename].route2
menu_name: main
weight: 2

[modulename].menulink_child2:
title: 'View 3'
description: 'View the Third'
parent: [modulename].menulink1
route_name: [modulename].route3
menu_name: main
weight: 3

3) Refresh the cache. As long as the routes are defined in your [modulename].routing.yml file, the menu will appear in the main menu block, and the parent menu will be auto-expanded.

0

Quick solution to expand the entire menu, you can do this with drush or phpmyadmin:

First get the machine-name of the menu you want to set expanded. To see a quick list use this query in phpmyadmin:

//In Phpmyadmin:
SELECT DISTINCT menu_name FROM menu_tree;

//with Drush: 
drush sqlq "SELECT DISTINCT menu_name FROM menu_tree;"

Once you know the machine name you can use this query to convert the entire menu to expanded:

//in Phpmyadmin:
UPDATE menu_tree SET expanded='1' WHERE menu_name='replace_menu_name_from_previous_query';

//with Drush:
drush sqlq "UPDATE menu_tree SET expanded='1' WHERE menu_name='replace_menu_name_from_previous_query';"

Afterwards it might be necessary to clear the caches.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.