0

I have the menu item id. I want to display that menu item with its children.

Suppose for example I have menu item test and it has two children menu items. So I want to load them and display it as

test
  test1
  test2

I tried to load the menu item with the following code.

$menu_content = current(\Drupal::entityManager()->getStorage('menu_link_content')->loadByProperties(array('id' => 1)));

But I am unable to get the children from the variable. Can someone help me here?

3
1

Try these:

// Select the parent menu link with 49 id.
$menu_link_content_id = 49;
// Use the 'menu_link_content' entity storage.
$menu_content_storage = \Drupal::entityTypeManager()->getStorage('menu_link_content');
/** @var \Drupal\menu_link_content\MenuLinkContentInterface $menu_link_content */
// Fetch an entity with parent menu link.
$menu_link_content = current($menu_content_storage->loadByProperties(['id' => $menu_link_content_id]));
// Build a specific value for a parent property.
$parent_prop = "menu_link_content:{$menu_link_content->uuid()}";
// Load child menu items by 'parent' property.
$menu_link_content_childs = $menu_content_storage->loadByProperties(['parent' => $parent_prop]);
0
-3

Try this:

 $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);
            $list = [];
            $i =0;
         foreach ($tree as $item) {
             $title = $item->link->getTitle();
             $url = $item->link->getUrlObject();
             $menu_url = $url->toString();
             $list[] = array('id'=> $i, 'value'=>$title ,'img_url' =>'' ,'menu_link_url' => $menu_url);
             $i++;
           }
1
  • 1
    Vikram, what exactly is this code doing? Please try to add at least some comments. Code without any explanation doesn't make a good answer.
    – leymannx
    May 14 '20 at 8:44

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