In Drupal 8, you can use system blocks to show menus starting from different levels. For example:

Menu item 1 (level1)
--- Menu Item 2 (level2)
--- Menu Item 3 (level2)

Menu item 4 (level1)
--- Menu Item 5 (level2)
--- Menu Item 6 (level2)

When user is on a page linked by menu item 2 I want to have a block showing the following:

Menu item 1 
    --- Menu Item 2
    --- Menu Item 3

What the main system block offers is the following result:

    --- Menu Item 2
    --- Menu Item 3

You see? I need the parent menu item of these menu items as well.

What I did so far is to define a custom block in my module:

class MainMenuBlockSecondLevel extends BlockBase {

  public function build() {
    $menu_name = 'main';
    $menu_tree = \Drupal::menuTree();
    $parameters = $menu_tree->getCurrentRouteMenuTreeParameters($menu_name);

    $tree = $menu_tree->load($menu_name, $parameters);
    $manipulators = array(
      array('callable' => 'menu.default_tree_manipulators:checkAccess'),
      array('callable' => 'menu.default_tree_manipulators:generateIndexAndSort'),
    $tree = $menu_tree->transform($tree, $manipulators);

    $parents = $parameters->expandedParents;  // menu_link_content:dd7b39a6-c4c8-48dd-a1cf-92a755087c29

    $build['parent']['#markup'] = '<div class="parent-menuitem">Parent</div>';
    $build['menu'] = $menu_tree->build($tree);
    return $build;

With the $parents variable I get an array like that:

    [0] => 
    [1] => menu_link_content:dd7b39a6-c4c8-48dd-a1cf-92a755087c29

Here it stops for me now. What is menu_link_content:dd7b39a6-c4c8-48dd-a1cf-92a755087c29 and how can I get the needed information like menu item title and path out of it?

1 Answer 1


MenuTreeParameters::$expandedParents are a list of menu tree ids (a composite key of the menu bundle and the menu content UUID). You can see its data in the menu_tree, menu_link_content, menu_link_content_data, database tables.

If you need to programmatically invoke the menu item for reference, you can load it via Drupal's entity manager:

// Load each menu content item per UUID
foreach ($parents as $menu_uuid) {
  $menu_content = current(\Drupal::entityManager()->getStorage('menu_link_content')->loadByProperties(array('uuid' => $menu_uuid)));
  // @TODO: Reference menu content properties for data
  // (e.g. $menu_content->get('title')->value / $menu_content->get('link')
  • Thank you very much for this information - I will go deeper into all this stuff with this information. My first question: as the value of each key in my $parents array as a composite of bundle and UUID is there a helper function to get just the UUID of such a construction? I used '$uuid = substr($parents[2], 18)' to get just the UUID part. Additionally $parents array has many items - how can I get just the direct parent of the current menu item? Commented Jun 15, 2016 at 16:10
  • Newer versions of Drupal use entityTypeManager() instead of entityManager(). Commented Mar 5, 2021 at 21:10

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.