Score:0

How to get a menu link UUID from a menu tree item?

cl flag

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.

Score:1
cn flag

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();
cl flag
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?
4uk4 avatar
cn flag
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.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.