From what I understand I should be able to retrieve all the items in a menu using menuTree. However I only get one level/depth no matter what I try.

My Menu Looks Like:

Imported Content
----A Child
------Grand Child
--------Great Grandchild
------Grand Child 2
----Child 2
--A Sibling

Menu structure (screenshot): https://www.evernote.com/l/AAKdnwekb7BM87AFANo1z2XjCdEvR8HgDEI

This is my code:

  $menu_name = 'imported-content-menu';  
  $menu_tree = \Drupal::menuTree();  
  $parameters = $menu_tree->getCurrentRouteMenuTreeParameters($menu_name);  
  $tree = $menu_tree->load($menu_name, $parameters); 

When I inspect $tree, it is an array of two items "Products" and "A Sibling".

I inspected these two items to see if there were nested items (the children) but there were not.

The "Products" item has 'hasChildren' set to TRUE. But I don't see those children.

  • Since hasChildren is true and maxDepth is plenty deep I believe there should be a $tree[%pluginid]->subtree array. However, my subtree array exists but is empty. To debug I took the pluginid for 'Products' and use it in setRoot. I got an empty tree. Maybe my issue is these children can't be loaded for some reason. I am not using access manipulators. Any tips how to debug this would be greatly appreciated.
    – maestrojed
    Dec 21, 2017 at 4:42

1 Answer 1


Within the typical route context ($menu_tree->getCurrentRouteMenuTreeParameters($menu_name)), subtree elements are loaded only, if the menu item is flagged as "Show as expanded" within the menu item settings.

To load the entire tree, you have to pass in a plain MenuTreeParameters object without the current route context:

use Drupal\Core\Menu\MenuTreeParameters;

$parameters = new MenuTreeParameters();

// Optionally limit to enabled items.

// Optionally set active trail.
$menu_active_trail = \Drupal::service('menu.active_trail')->getActiveTrailIds($menu_name);

// Load the tree.
$tree = \Drupal::menuTree()->load($menu_name, $parameters);
  • You have no idea how much thanks I have to give! Thank you!!! This was the issue.
    – maestrojed
    Dec 21, 2017 at 14:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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