0

I'm trying to create a select box like the one that allows the selection of a menu parent on the node form. I might be barking up the wrong tree in my approach. I figured if I can get an array of menu link titles and UUIDs I will have what I need.

I tried this:

$menus = Menu::loadMultiple();
$menu_tree_service = \Drupal::service('menu.link_tree');
foreach($menus as $menu) {
  $id = $menu->id();
  $parameters = new \Drupal\Core\Menu\MenuTreeParameters();
  $parameters->setMaxDepth(2);
  $tree = $menu_tree_service->load($id, $parameters);
  foreach ($tree as $item) {
    $title = $item->link->getTitle();
  }
}

This does give me the menu link title, but I see no path to the UUID, and I believe I need that downstream to dynamically add a child link at that position.

1 Answer 1

1

The UUID of menu links defined in content, not in code, is the derivative ID of the menu link plugin:

$item->link->getDerivativeId();

For setting a parent you probably want the entire plugin ID, which includes the UUID for a content menu link:

$item->link->getPluginId();
2
  • Shouldn't a UUID be numeric? I'm getting pluginid values like: admin_toolbar_tools.extra_links:entity.block_content_type.edit_form.basic, contact.site_page, etc. Is it only content that has the numeric UUIDs?
    – JAyenGreen
    Jun 16, 2023 at 11:25
  • No, a derivative ID doesn't need to be numeric. This is only the case if it is derived from an entity and then the entity UUID is used. If the derivative ID is empty then the plugin is not derived and probably defined in a YAML file. If it is non-numeric then it is derived from something else which doesn't use a numeric identifier. And yes, in this context it's only content that has numeric UUIDs.
    – 4uk4
    Jun 16, 2023 at 11:57

Your Answer

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

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