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
| Comment | File | Size | Author |
|---|---|---|---|
| #7 | token-3332659-menu-link-content-bundle-7.patch | 899 bytes | alex.skrypnyk |
| #3 | token-3332659-menu-link-content-bundle-1.patch | 1023 bytes | murz |
Issue fork token-3332659
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
Comment #3
murzI'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.
Comment #4
murzComment #5
murzAlso, I doubt, do we really need to duplicate the behavior of
menu_uimodule of creating a menu item from Drupal core into thetoken.module? For what purposes it is there?Comment #7
alex.skrypnykRe-rolled for 1.16