Skip to main content
added 182 characters in body
Source Link
MilanG
  • 569
  • 7
  • 28

I'm struggling to figure out relationship between MenuLink and MenuLinkContent.

I'm creating new menu item like:

$menuItem = MenuLinkContent::create($params);

I can also query those elements like:

$menuItems = \Drupal::entityTypeManager()->getStorage('menu_link_content')->loadByProperties(['link.uri' => "internal:$path"]);

And that works well. Now if I need one of those object I want to get it's "siblings" - other object that share same parent. I have some weird structure:

$this->menuLinkManager = \Drupal::service('plugin.manager.menu.link');
$parentId = $menuItem->getParentId();
$parent = $this->menuLinkManager->createInstance($this->menuLinkManager->getDefinition($parentId)['id']);

And here I'm getting it's parent, but it's not MenuLinkContent object any more but instance of MenuLinkInterface. It has some fields as MenuLinkContent, but not all. I.e. I don't know how to get link path and similar.

There I can go further:

$siblings = $this->menuLinkManager->getChildIds($parent->getPluginId());

But main question is how to get MenuLinkContent when I have that MenuLinkInterface object?

Sub-question - why oh why is needed this parallel menu system and parallel menu classes?

I'm struggling to figure out relationship between MenuLink and MenuLinkContent.

I'm creating new menu item like:

$menuItem = MenuLinkContent::create($params);

And that works well. Now if I need one of those object I want to get it's "siblings" - other object that share same parent. I have some weird structure:

$this->menuLinkManager = \Drupal::service('plugin.manager.menu.link');
$parentId = $menuItem->getParentId();
$parent = $this->menuLinkManager->createInstance($this->menuLinkManager->getDefinition($parentId)['id']);

And here I'm getting it's parent, but it's not MenuLinkContent object any more but instance of MenuLinkInterface. It has some fields as MenuLinkContent, but not all. I.e. I don't know how to get link path and similar.

There I can go further:

$siblings = $this->menuLinkManager->getChildIds($parent->getPluginId());

But main question is how to get MenuLinkContent when I have that MenuLinkInterface object?

Sub-question - why oh why is needed this parallel menu system and parallel menu classes?

I'm struggling to figure out relationship between MenuLink and MenuLinkContent.

I'm creating new menu item like:

$menuItem = MenuLinkContent::create($params);

I can also query those elements like:

$menuItems = \Drupal::entityTypeManager()->getStorage('menu_link_content')->loadByProperties(['link.uri' => "internal:$path"]);

And that works well. Now if I need one of those object I want to get it's "siblings" - other object that share same parent. I have some weird structure:

$this->menuLinkManager = \Drupal::service('plugin.manager.menu.link');
$parentId = $menuItem->getParentId();
$parent = $this->menuLinkManager->createInstance($this->menuLinkManager->getDefinition($parentId)['id']);

And here I'm getting it's parent, but it's not MenuLinkContent object any more but instance of MenuLinkInterface. It has some fields as MenuLinkContent, but not all. I.e. I don't know how to get link path and similar.

There I can go further:

$siblings = $this->menuLinkManager->getChildIds($parent->getPluginId());

But main question is how to get MenuLinkContent when I have that MenuLinkInterface object?

Sub-question - why oh why is needed this parallel menu system and parallel menu classes?

Source Link
MilanG
  • 569
  • 7
  • 28

MenuLink vs MenuLinkContent - what's the point?

I'm struggling to figure out relationship between MenuLink and MenuLinkContent.

I'm creating new menu item like:

$menuItem = MenuLinkContent::create($params);

And that works well. Now if I need one of those object I want to get it's "siblings" - other object that share same parent. I have some weird structure:

$this->menuLinkManager = \Drupal::service('plugin.manager.menu.link');
$parentId = $menuItem->getParentId();
$parent = $this->menuLinkManager->createInstance($this->menuLinkManager->getDefinition($parentId)['id']);

And here I'm getting it's parent, but it's not MenuLinkContent object any more but instance of MenuLinkInterface. It has some fields as MenuLinkContent, but not all. I.e. I don't know how to get link path and similar.

There I can go further:

$siblings = $this->menuLinkManager->getChildIds($parent->getPluginId());

But main question is how to get MenuLinkContent when I have that MenuLinkInterface object?

Sub-question - why oh why is needed this parallel menu system and parallel menu classes?