2

I created Menu Link Content entities and saved them in a menu.

$menu_link = MenuLinkContent::create([
  'title' => 'Menu item',
  'bundle' => 'menu_link_content',
  'link' => [['uri' => 'internal:/']],
  'menu_name' => 'testing',
])->save();

$menu_link_2 = MenuLinkContent::create([
  'title' => 'Menu item',
  'bundle' => 'menu_link_content',
  'link' => [['uri' => 'internal:/']],
  'menu_name' => 'testing',
])->save();

How can I rearrange $menu_link_2 to be a child of $menu_link?

Looking at menu_ui, they seem to update Menu Link Content in a given order, and then rebuild the menu tree; finally, the Menu entity is saved.

$menu_link_manager = \Drupal::service('plugin.manager.menu.link');
$menu_link_manager->updateDefinition($menu_link_2->getPluginId(), [
  'parent' => $menu_link->getPluginId(),
]);

How can I rebuild the menu tree and save the menu?

Why? I'm writing a Kernel test for a module that fires when the menu link content has been saved AND then when the Menu itself has been saved.

1 Answer 1

0

You can rearrange a custom menu link when you set the 'weight' of the menu. See below code.


$menu_link = MenuLinkContent::create([
  'title' => 'Menu item',
  'bundle' => 'menu_link_content',
  'link' => [['uri' => 'internal:/']],
  'menu_name' => 'testing',
  'weight' => 0,
])->save();

$menu_link_2 = MenuLinkContent::create([ 'title' => 'Menu item', 'bundle' => 'menu_link_content', 'link' => [['uri' => 'internal:/']], 'menu_name' => 'testing', 'weight' => 0, ])->save();

2
  • 1
    that's not what he's asking for, it's about nesting, not the weight
    – David
    May 8, 2017 at 7:52
  • yes and no. In the title its about sorting. I came here for sorting an this is just what i wanted to know.
    – snitch182
    Jan 3 at 8:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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