When multiple menu items point to the same page, the active trail is often not the intended one. This happens a lot. It is very difficult that a breadcrumb and a URL are singular but a menu item can be multiple.
The trouble comes in menu_link_get_preferred. Various solutions have been presented such as preferring the item that is most deeply nested #306412: Get the deepest menu entry when more than one entry points to the same content or by having multiple active trails #609542: Active trail isn't set on all menu items pointing to the current path.
One simple improvement I think we should make is to, for a node, favor the per-node menu settings over the 'customized' menu items made via menu administration. Because only one menu item can be made on a node form, treating this item as canonical relative to active trail (like we treat its path alias setting there as canonical) makes a lot of sense and is easy for editors to understand.
What we currently have is no real ordering of which menu item to prefer in an active path, so adding a bit of ordering that makes sense to users seems like a big step forward. And also a one line patch. Creds to Timmy P.
| Comment | File | Size | Author |
|---|---|---|---|
| #13 | menu-active-trail-consistency-option1-1875824-13.patch | 1.15 KB | khaled.zaidan |
| #10 | menu-active-trail-consistency-option2-1875824-10.patch | 989 bytes | khaled.zaidan |
| #10 | menu-active-trail-consistency-option1-1875824-10.patch | 1.15 KB | khaled.zaidan |
| #3 | core-1875824-3-d7.patch | 535 bytes | barancekk |
| menu-order-customized.patch | 555 bytes | jody lynn |
Comments
Comment #2
tim.plunkettmenu-order-customized.patch queued for re-testing.
Comment #3
barancekk commentedI edited the patch to make it work on 7.22 version of drupal. Attaching in this comment.
Thank you
Comment #5
stefan.r commented#1649062: Core is inconsistent when handling two menu items with the same path. It says one thing in the UI and does another in reality.
Comment #6
tim.plunkettCan you explain how this is a duplicate? That's a 7.x issue.
Comment #7
stefan.r commentedSorry, I had missed that this was a D8 issue with the D7 patch and all, the other issue contained a patch which did essentially the same.
How would this be implemented in D8? "customized" seems to have been replaced by MenuLinkInterface::isResettable(), but that doesn't mean the same thing as "customized" any more.
Comment #10
khaled.zaidan commentedLooking at this issue in D8.
I've found a possible fix attached in the patch (actually 2 potential fixes, hence the 2 patches).
The issue happens when the MenuActiveTrail service decides what the active trail is. This happens in Drupal\Core\Menu\MenuActiveTrail::getActiveLink(). We can see in that function, loadLinksByRoute() returns all the links the match the current route, and then simply the first one of the links is chosen as the active trail.
So we can either:
1) Instead of simply picking the first link here, we do something smarter to pick the one that appears in the node form (I'll get to how in a below).
Or
2) Make loadLinksByRoute() give preference to the link that appears on the node form.
So I looked at how the link that appears on the node form is chosen. And found that it's chosen in menu_ui_get_menu_link_defaults($node), which sorts links by mlid (or id in the menu_link_content table).
So we can mimic that behaviour by either choosing the link with lowest mlid in Drupal\Core\Menu\MenuActiveTrail::getActiveLink() or by sorting ascendingly by mlid in loadLinksByRoute().
I looked through the code for uses of loadLinksByRoute(), and didn't find any functionality (in core at least) using this other than the active trail. And there doesn't seem to be any reason for the specific order links currently come back in. So I see no harm in changing it, and for the sake of consistency across any potential related functionalities, I think going with this option (option 2) makes more sense.
Anyway, I hope someone could have a look at these patches, and hopefully we see this issue fixed soon.
PS. Attached patches are against 8.2.x-dev.
Comment #11
gmclelland commentedPatched attached. Setting to NR
Comment #13
khaled.zaidan commentedOh, I'm feeling silly now, I had a bit of bad code in the patch for option 1 here:
Attached is a modified version.
Comment #26
smustgrave commentedThank you for creating this issue to improve Drupal.
We are working to decide if this task is still relevant to a currently supported version of Drupal. There hasn't been any discussion here for over 8 years which suggests that this has either been implemented or is no longer relevant. Your thoughts on this will allow a decision to be made.
Since we need more information to move forward with this issue, the status is now Postponed (maintainer needs more info). If we don't receive additional information to help with the issue, it may be closed after three months.
Thanks!
Comment #28
smustgrave commentedCode has changed since then but looking at it still seems to only return the first item. So not comfortable closing this one so putting back.