Bug with menu item uuid when first save a node

Created on 27 February 2019, over 5 years ago
Updated 31 July 2023, 11 months ago

Some times ago, a bug was fixed with token, when a node was saved for the first time:
https://www.drupal.org/project/token/issues/2670120 β†’

The patch contains this code:

        if ($node->isNew()) {
          // Create a new menu_link_content entity.
          $entity = MenuLinkContent::create([
            // Lets just reference the UUID for now, the link is not important for
            // token generation.
            'link' => ['uri' => 'internal:/node/' . $node->uuid()],
            'langcode' => $node->language()->getId(),
          ]);
        }
        else {
          // Create a new menu_link_content entity.
          $entity = MenuLinkContent::create([
            'link' => ['uri' => 'entity:node/' . $node->id()],
            'langcode' => $node->language()->getId(),
          ]);
        }

The first, time, a node is saved, a menu item is created with the following URI:
"internal:/node/2f6c9f74-4a41-4366-b2b8-69d6a93fa798"

If you want to edit such a menu item, you will get a fatal error unless you have applied the following patch in Drupal core:

        "patches": {
            "drupal/core": {
                "Menu Link UUID": "https://www.drupal.org/files/issues/2019-01-07/drupal-link_uuid-2353611-248-d8.patch"
           },
}

This patch will allow the used url format "internal:/node/{uuid}".

But then you will run in another issue: The second time you edit the node, the reference to the menu link is completly lost.
The menu ui module has the following code to find the corresponding menu item:

    // Check all allowed menus if a link does not exist in the default menu.
    if (!$id && !empty($type_menus)) {
      $query = \Drupal::entityQuery('menu_link_content')
        ->condition('link.uri', 'entity:node/' . $node->id())
        ->condition('menu_name', array_values($type_menus), 'IN')
        ->sort('id', 'ASC')
        ->range(0, 1);
      $result = $query->execute();

      $id = (!empty($result)) ? reset($result) : FALSE;
    }

As you can see, the new temporary url format is not supported. To fix this, I created an additional patch.

Only with these two patches, you will be able to use to menu tokens on nodes working the first time and not being deleted the second time a node is saved.

πŸ› Bug report
Status

Needs review

Version

1.0

Component

Code

Created by

πŸ‡¨πŸ‡­Switzerland ayalon

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.69.0 2024