Problem/Motivation
Menus don't respect unchecking the "Show as expanded" option when you're viewing a page in the active trail, in those cases, the menu item is treated as though you had checked "show as expanded."
Steps to reproduce
- Add menu item and set show as expanded to false.
- Add sub menu items to the menu item.
- Navigate to some random page with the menu and see that the menu item is not expanded.
- Navigate to a page in the active trail, i.e. a page for one of the sub menu items and see that the menu item is treated as expanded even though it was not configured to be "shown as expanded"
This seems to be very similar to #3055340: Menu item checkbox "Show as expanded" not working properly, probably a duplicate.
Proposed resolution
It seems that this is a hardcoded decision that if viewing a page in the active trail, all parent menu items are shown as expanded as long as they are within the max-depth requirements of the menu. But should this be the case? It seems like it might be a more intuitive behavior to only expand the items if "show as expanded" is checked. (Or if the menu block is forcing all items to be "shown as expanded".)
Remaining tasks
- Update code to respect the "show as expanded" option, even on pages in the active trail.
User interface changes
API changes
Data model changes
Release notes snippet
| Comment | File | Size | Author |
|---|---|---|---|
| #7 | 3456536-fix-menu-expansion-7.patch | 976 bytes | ibrahim tameme |
| #6 | 3456536-fix-menu-expansion-6.patch | 978 bytes | ibrahim tameme |
| #5 | 3456536-fix-menu-expansion.patch | 711 bytes | ibrahim tameme |
Issue fork drupal-3456536
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
ibrahim tameme commentedComment #5
ibrahim tameme commentedComment #6
ibrahim tameme commentedAfter checking it turn out that all set as expanded option in Administration menu links are set to false, so with the patch provided above you will need to update all the Administration menu links which is a rough task so this patch ignores the Administration menu and add the condition to other menus.
Comment #7
ibrahim tameme commentedComment #8
arun.k commented@Ibrahim Tameme i installed drupal 10.3.x on DDEV v1.22.6 and php 8.3.0 and apply the patch 3456536-fix-menu-expansion-7.patch which is working fine.
Comment #9
quietone commentedThis may be a duplicate of #3055340: Menu item checkbox "Show as expanded" not working properly.
I tested this on Drupal 11.x, using the steps in the issue summary. I did not see any difference in the UI between when the patch was not applied and when it was. Is there supposed to be a change?
Changes are made on on 11.x (our main development branch) first, and are then back ported as needed according to our policies.
Comment #10
aaronchristian commentedThis seems to fix the issue for me.
Previously
{{ item.is_expanded }}was being forced totruewhen on the active menu item.Debugging it reveals false when on the active menu item and it has children, even when its not set to "expanded".
After applying the patch;
And then just to ensure your menu doesn't print out, ensure you have some logic like;
Thanks for the patch!
Comment #11
sonfdComment #12
sonfdComment #13
sonfdI tried to update the issue summary to be a little more clear. I do think this is a duplicate of #3055340: Menu item checkbox "Show as expanded" not working properly, but I think the approach in this issue is the correct way to go so I'd recommend closing the other one.
To @aaronchristian's point, I think this patch is only complete with a change to the template file. Maybe I'm wrong, but I think we shouldn't even be building the subtrees of a menu item if it's not expanded. And if we do that, we won't need a change to a template file. I'll take a stab at that in an MR now.Comment #14
mortona2k commentedThey read as duplicates to me, both are talking about confusion over the option and how it affects hardcoded behavior.
I was recently diving into how menus load, and learned about using MenuLinkTree service to load the menu. At first I was trying to stuff extra content into the items with a custom modifier for MenuLinkTree->transform(). But then I discovered that those are more for controlling the structure of the menu, like removing links you don't have access to, or changing the order. But loading additional content is intended to be in a preprocess.
I took notes on things I discovered here: https://www.drupalarchitect.info/articles/rendering-menus
I think the question is where we should make the decision on how menus render. If everything is available, the decision can be made in the template. If the child links are removed further back in the render pipeline, they take deeper development to modify. For example, adding a custom handler to adjust the link loading takes way more effort than deciding not to print level 3 items in a template file.
That said, I think default menu templates should be able to handle whatever links are passed in, and the UI should give us clear control over what is happening per menu display.
Comment #15
sonfdActually, scratch pretty much everything I just said.
I'm seeing in MenuLinkTree::getCurrentRouteMenuTreeParameters(), we are explicitly expanding links in the active trail:
If we're going to do something here, I think we have to do it there or change the behavior in SystemMenuBlock, which is where this gets called from.
Comment #16
mortona2k commentedDoing it in SystemMenuBlock makes sense to me, that's where the "Expand all menu links" checkbox is. An additional checkbox for "Don't auto-expand items in the active menu trail"?
Comment #17
sonfd@mortona2k, I was just coming back to this after sleeping on this to say basically the same thing! Except I think I'd recommend the additional checkbox be "Expand items in the active trail" and we can default it to checked.
Comment #18
mortona2k commentedThat makes more sense.