Problem/Motivation
MenuLinkManager::updateDefinition() currently saves the menu link entity before updating the menu tree when $persist is TRUE. This ordering causes entity hooks (e.g., pathauto's hook_entity_update) to fire while the menu tree still contains stale parent/child data. Modules that react to menu link entity saves and query the menu tree (like Pathauto for URL alias generation) receive incorrect tree information, leading to wrong menu hierarchy or broken path aliases.
Steps to reproduce
Create a parent-child menu link hierarchy using MenuLinkContent entities
Install a module (e.g., Pathauto) that implements hook_entity_update on menu_link_content and reads the menu tree
Call MenuLinkManager::updateDefinition() to change a child link's parent with $persist = TRUE
Observe that the entity hook fires before the tree is updated — the hook sees the old tree structure
Proposed resolution
In MenuLinkManager::updateDefinition(), change the order of operations:
Call updateLink() with $persist = FALSE first — this updates the in-memory definition without persisting the entity
Save the tree via MenuTreeStorage::save() with the updated definition
If the original $persist was TRUE, call updateLink() again with $persist = TRUE to persist the entity
This ensures the menu tree is always updated before entity hooks fire, so any hook (Pathauto or otherwise) that queries the tree during entity save sees the correct, updated hierarchy.
| Comment | File | Size | Author |
|---|---|---|---|
| #12 | 3181070-12.patch | 1.16 KB | marckwee |
| #2 | 3181070-possible-need-to-change-the-way-menulinkmanager-updates-menu-item-and-menu-tree.patch | 1.16 KB | sylvain lavielle |
Issue fork drupal-3181070
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 #2
sylvain lavielle commentedHere's a patch.
Disclaimer : This is a quick fix approach, I'm really aware that the proper solution would be more tricky. But this worked for me in conjunction with https://www.drupal.org/project/pathauto/issues/3016532#comment-13885277. It can be a valid workaround for people the time pathauto and possibly drupal core reach a perfect solution about this issue
Comment #3
simon georges commentedChanging status to "Needs review", as there is a patch.
Comment #5
mr.york commentedThank you! Good patch.
Comment #6
catchAs explained by the patch author in #2 this isn't a committable patch - it might point to something going wrong, or there might be an alternative fix for pathauto.
Comment #12
marckwee commentedHi,
We still needed this patch and we are upgrading to D11, so here an new version. It is the same patch logic wise.
It still goes "wrong" when you manually drag-drop menu items, this patch should fix that.
Comment #15
phthlaap commentedComment #16
smustgrave commentedIssue summary tag is still relevant.
Please reach full issue and tags before moving to review
Comment #17
phthlaap commentedComment #18
phthlaap commented