0

Let's say my menu tree is like below:

  • Parent
    • Sub-Parent
      • Child

I want to get the Sub-Parent title and all child's title and url.

How can I do it in Drupal 8?

I have looked at this answer but it just for getting the parent, it is not the one I'm looking for.

0

Given the answer you mentioned, I assume that you would like to retrieve your items data programmatically.

What you are missing is the setRoot() method from MenuTreeParameters (here). Here is how you could start with:

    $menuTree = \Drupal::menuTree();

    $menuTreeParameters = new MenuTreeParameters();
    // This will fetch the tree starting from your Sub-Parent
    $menuTreeParameters->setRoot('sub_parent_link_plugin_ID');
    // Set the nb of levels, starting from root you defined, that you want to fetch (0 = all)
    $menuTreeParameters->setMaxdepth(1);

    // Load tree based on your parameters
    $myMenuTree = $menuTree->load('name_of_your_menu', $menuTreeParameters);
    // List of Sub-parent children items
    $subtree = $myMenuTree['sub_parent_link_plugin_ID']->subtree;

$myMenuTree will give you data about Sub-Parent as a MenuLinkTreeElement. $subtree will correspond to its children, each as a MenuLinkTreeElement as well.

In each of these two variables, you will find for each MenuLinkTreeElement a link entry of type MenuLinkContent containing all the data about your menu link item. Starting from link, you would be able to use the MenuLinkContent methods to retrieve the title and url of each of your items.

Hope this helps.

  • Sorry for reply late because I was trying other solutions these few days. And thank you so much! Your solution is the most easiest one and it works perfectly for me :) – gdmorning520 Jun 11 '19 at 3:42
0

You can do this by configuring the Menu levels on your Menu Block and placing that Block in a region of your theme. Additionally configure the Visibility of the block so it only appears on pages where you want it.

You can learn more about the Menu levels configuration here: Drupal 8 Navigation Menu Blocks configuration.

In your case you want the Initial visibility level and Number of levels to display to be either:

1 and 3

OR

2 and 3

AND

you'll want the Expand all menu items to be checked, unless you have all the items Expanded under Menu settins.

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.