Problem/Motivation

Token module has the function token_node_menu_link_submit() that creates a new MenuLinkContent entity.

Historically, Drupal has a pre-filled bundle menu_link_content, but the patch from the #2987537: Custom menu link entity type should not declare "bundle" entity key fixes that, but after applying - the Token module produces the error:

The website encountered an unexpected error. Please try again later.

Drupal\Core\Entity\EntityStorageException: Missing bundle for entity type menu_link_content in Drupal\Core\Entity\ContentEntityStorageBase->doCreate() (line 125 of core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php).

Drupal\Core\Entity\ContentEntityStorageBase->create(Array) (Line: 1143)
Drupal\Core\Entity\ContentEntityBase::create(Array) (Line: 736)
token_node_menu_link_submit('node', Object, Array, Object)

Steps to reproduce

1. Get the default clean Drupal installation, install the Token module.
2. Apply the patch from the #2987537: Custom menu link entity type should not declare "bundle" entity key .
3. Go to /node/add/page
4. Check the "Provide a menu link".
5. Press "Save" and see the fatal error.

Proposed resolution

Seems the fix should be the same - just fill the bundle via the right value:

        if ($node->isNew()) {
          // Don't create a menu link if the node is being previewed.
          if ($form_state->getTriggeringElement()['#id'] == 'edit-preview') {
            return;
          }
          // 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.
+           'bundle' => $values['menu_name'],
            'link' => ['uri' => 'internal:/node/' . $node->uuid()],
            'langcode' => $node->language()->getId(),
          ]);
        }
        else {
          // Create a new menu_link_content entity.
          $entity = MenuLinkContent::create([
+           'bundle' => $values['menu_name'],
            'link' => ['uri' => 'entity:node/' . $node->id()],
            'langcode' => $node->language()->getId(),
          ]);
        }
      }

Remaining tasks

User interface changes

API changes

Data model changes

Issue fork token-3332659

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

Murz created an issue. See original summary.

murz’s picture

Title: Token module doesn't fill the right bundle for MenuLinkContent entity on token_node_menu_link_submit » 3332659-menu-link-content-bundle
Status: Active » Needs review
StatusFileSize
new1023 bytes

I've made an MR with the changes, please review.

Also, attaching a static patch file with those changes, to make it possible to use in composer until #3204538: GitLab Merge Requests Unable to Generate Incremental Patch Files is resolved.

murz’s picture

Title: 3332659-menu-link-content-bundle » Token module doesn't fill the right bundle for MenuLinkContent entity on token_node_menu_link_submit
murz’s picture

Also, I doubt, do we really need to duplicate the behavior of menu_ui module of creating a menu item from Drupal core into the token.module? For what purposes it is there?

Status: Needs review » Needs work

The last submitted patch, 3: token-3332659-menu-link-content-bundle-1.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

alex.skrypnyk’s picture

Re-rolled for 1.16