Change record status: 
Project: 
Introduced in branch: 
10.2.x
Introduced in version: 
10.2.0
Description: 

getEntity method of Drupal\menu_link_content\Plugin\Menu\MenuLinkContent is public now so it can be used to fetch the menu_link_entity entities from the menu link content plugin.

$menu_link = MenuLinkContent::create([
    'title' => $title,
    'link' => [['uri' => 'internal:/' . $user->toUrl('collection')->getInternalPath()]],
    'menu_name' => 'menu_test',
  ]);
  $menu_link->save();
  $menu_tree = \Drupal::menuTree()->load('menu_test', new MenuTreeParameters());

In the above $menu_tree each element is instance of Drupal\menu_link_content\Plugin\Menu\MenuLinkContent
From each tree element, we can access the link like $tree_element->link

From the $tree_element->link we can call getEntity() method and it will return the MenuLinkContent entity.

So, we can add additional things in above snippet to get the entity.

foreach ($menu_tree as $tree_element) {
    $link = $tree_element->link;
    $menu_link_content_entity = $tree_element->link->getEntity();
    $menu_link_content_entity_title = $tree_element->link->getEntity()->getTitle();
}

Impacts: 
Module developers

Comments

liam morland’s picture

Note that there are two classes called MenuLinkContent:

  • \Drupal\menu_link_content\Entity\MenuLinkContent
  • \Drupal\menu_link_content\Plugin\Menu\MenuLinkContent