Problem/Motivation
Right now using the core menu block subtrees aren't show unless they are in the active trail. This isn't ideal for things like primary navigation which require subtrees to be shown regardless of which item the user is viewing.
As stated in #95:
Here's an example of why the proposed feature is very useful, as it allows configurations like this setup:
- Top 1
- Sub 1
- Sub 2
- Top 2
- Sub 3
- Top 3
- Sub 4
- Sub 5
Goals:
- The main menu shall display the top two levels for the page's main navigation
- The main menu shall follow the active trail for secondary navigation, starting at the top level as well.
To achieve the first using the default behavior, we must mark all "Top" items as expanded. If we visit the page of "Top 1", the secondary navigation block will display all 5 "Sub" items, since their parents are all expanded. With the proposed patch, the menu items no longer need to be expanded, and instead the primary block can be set to "Expand all". The secondary block will then only display the three "Top" items and "Sub 1" + "Sub 2".
Proposed resolution
Add an option to the core system menu block to make this behavior configurable.
Remaining tasks
User interface changes
Screenshot of menu with sublinks without the box checked:
Screenshot of menu with the box checked:
Screenshot of the UI with the checkbox:
API changes
None
Data model changes
None
Comment | File | Size | Author |
---|---|---|---|
#138 | 2594425-follow-up.patch | 730 bytes | jibran |
#126 | 2594425-126.patch | 62.53 KB | Sam152 |
#126 | interdiff.txt | 2.08 KB | Sam152 |
#120 | 2594425-119.patch | 62.31 KB | Sam152 |
#120 | interdiff.txt | 1.92 KB | Sam152 |
Comments
Comment #2
Sam152 CreditAttribution: Sam152 commentedQuick patch that is working for my use case.
Comment #3
Nitesh Pawar CreditAttribution: Nitesh Pawar at Trigyn Technologies Ltd commentedComment #5
Nitesh Pawar CreditAttribution: Nitesh Pawar at Trigyn Technologies Ltd commentedComment #6
Nitesh Pawar CreditAttribution: Nitesh Pawar at Trigyn Technologies Ltd commentedComment #8
Sam152 CreditAttribution: Sam152 commentedI think the failing tests are problem some exported config in the tests or even in the core profiles which are missing the key. If this feature and approach were validate, core config related to menu block placements would probably need a re-export.
Comment #9
Bojhan CreditAttribution: Bojhan commentedComment #10
jibranNW for tests.
Comment #11
jibranDo we need an upgrade path to update the config of existing blocks? I think yes.
Comment #12
jibranDo we need an upgrade path to update the config of existing blocks? I think yes.
Comment #13
dawehnerYes, we should set the default value to FALSE for all those blocks.
We are missing to change
\Drupal\system\Plugin\Block\SystemMenuBlock::defaultConfiguration
Comment #14
Sam152 CreditAttribution: Sam152 commentedI'll have a look at writing a test for this as well as setting the defaults.
Comment #15
4aficiona2 CreditAttribution: 4aficiona2 commentedIf I understand you correct this should already be possible if you flag every menu item which has children as 'expanded'.
I also ran into this problem (documented here) and the children got only rendered if they were part of the active trail. But my main nav. works as hover-menu and needs to get rendered always with all children (only first two levels). I also opened an issue which I'm not sure if it is exactly the same. What do you think?
In general it would definitely be more intuitive and usable if you could define this on the block instead of the menu item which makes the menu itself more flexible. Because there could be several menu block instances of the same menu with different settings, e.g. one expanded and one only toplevel items.
Comment #16
Sam152 CreditAttribution: Sam152 commentedMy use case was a menu in the sidebar which expanded and followed the active trail and a primary menu with everything expanded. This means you can't expand every item, because then the sidebar doesn't work.
I agree this should be on the block instance settings, which is #8 deals with.
Comment #17
4aficiona2 CreditAttribution: 4aficiona2 commentedI see, if this would be possible and the block configuration includes those menu settings (I remember menu_block module back in D7 had those abilities) I could also solve my issue posted.
Comment #18
Sam152 CreditAttribution: Sam152 commentedComment #20
Sam152 CreditAttribution: Sam152 commentedComment #21
jibranNice work @Sam152
We need to update the schema of all existing SystemMenuBlock instances as well.
And
this is still not addressed.
Comment #22
Sam152 CreditAttribution: Sam152 commentedDoes this really need a database update? Won't the default value be enough in this context?
Comment #23
Sam152 CreditAttribution: Sam152 commentedComment #25
jibranYes, whenever we add a new key to schema we have to update the existing active config schema so that everything remains in sync.
Comment #26
naveenvalechafixing tests.
N/W for update test.
Comment #27
naveenvalechaAdded empty update hook.
Comment #28
naveenvalechaadding removed tag(accidently removed)
Comment #29
naveenvalechaSetting to N/W for update & update path test.
Comment #30
naveenvalechaunassiging from sam as he's not working anymore on it.
Waiting for jibran to provide any example for update hook to update only those menu block plugins.
Comment #31
Sam152 CreditAttribution: Sam152 commentedThanks for picking this up.
Comment #32
naveenvalechawe have now upgrade path and upgrade path test.
Please also give credit to jibran for reviewing and helping in pushing this issue.
Comment #33
naveenvalechacorrect interdiff
Comment #34
naveenvalechaDrafted CR b/c we are adding new option, CR needs updation https://www.drupal.org/node/2669550
Comment #35
jibranLooks perfect.
Comment #39
Sam152 CreditAttribution: Sam152 commentedOne of these passed. Tests seem unrelated. Random fail?
Comment #40
swentel CreditAttribution: swentel commentedLooks random indeed.
Not sure if this is really relevant to check
Comment #41
naveenvalecha#39, RT
#40,
Don't wanna do the check for all blocks, only need to take care of plugin system_menu_block
That's check was for taking care of that.
Edit : That's check was needed for checking that 8.0.x does not have the
expand_all_items
key in schema.Not setting back to RTBC per #35, leaving for swentel, may be he has more thoughts
Comment #42
mishradileep CreditAttribution: mishradileep as a volunteer and commentedIts working but it was hard to understand the issue/feature.
Finally I figure it out and trying to explain with screenshots below to help others to understand.
Comment #43
dman CreditAttribution: dman as a volunteer and at Sparks Interactive commentedAdded screenshot to Issue Description
Comment #44
naveenvalechaBack to RTBC as per #35
Comment #45
Wim LeersI'm afraid this is entirely the wrong way to do this.
\Drupal\system\Plugin\Block\SystemMenuBlock::build()
generates aMenuTreeParameters
object by doing:If we look at that method, we see this:
So this patch first lets
\Drupal\Core\Menu\MenuLinkTree::getCurrentRouteMenuTreeParameters()
spend a lot of time retrieving menu items marked as "expanded", by querying the DB. And then this patch overwrites all that work by calling the new (poorly named)emptyExpandedParents()
method.That's just backwards.
This should either improve
\Drupal\Core\Menu\MenuLinkTree::getCurrentRouteMenuTreeParameters()
itself or improveSystemMenuBlock
's usage of that. We don't needemptyExpandedParents()
at all.This is an integration test. We also need a unit test for this in
MenuLinkTreeTest
.Comment #47
gappleThis should address the issues in #45
- If block is configured to display an expanded menu, it creates the MenuTreeParameters itself with only the active trail, and not ExpandedParents. If not expanded, parameter creation is delegated to MenuLinkTree.
- A unit test is added to ensure that the full menu is displayed at any level, when both the active trail is empty or is set.
Comment #49
gappleI mistakenly made the last patch against 8.1.x. Here's a re-roll against 8.2.x.
Comment #50
dawehnerJust some few nitpicks, the patch itself really a) Makes sense, there are IMHO quite some usecases for it and b) functionality wise, its looking great.
Is it just me or is this description not really helpful? I think the title itself tells you enough
Nitpick, this is no longer needed
I think it would be helpful to post to the actual instance:
system_post_update_add_expand_all_items_key_in_system_menu_bloc
I just quickly ensured that standard profile actually ships with blocks by default:
nitpick: let's use uppercase FALSE
the actual function is assertTrue ... PHP is simply caseInSenSItive. Should we check that the value is FALSE by default, as well?
Note: We are updating from 8.1 to 8.2 actually
<3 I love this kernel test!
Let's use sprintf or just "Menu block $id". Format_string is deprecated and shouldn't be used in tests
Comment #51
dawehnerComment #52
andypostyep, makes no sense and active trail confusing newcomers
Comment #53
gappleI removed the description on the 'Expand All Items' checkbox, and also moved it above the 'Menu Levels' fieldset so that it appears right after the 'Display title' checkbox.
Comment #55
dawehnerSorry for being a litte bit annoying :) What do you think about my new suggestions for the label?
This is the first occurrence of the word "tree" on this page, so I'm wondering whether "Expand all menu items" would be easier to understand
Let's skip this CS only changes. They can be fixed as part of #2571965: [meta] Fix PHP coding standards in core
Small tip: Always try to define variables in the smallest distance where they are needed. In this case it would be a little bit more readable to first define
$parameters
and then$active_trail
Comment #56
gappleMakes sense to me.
Updated label and remove code style changes.
Comment #57
dawehner@gapple
Can you post interdiffs so people can follow up what you did?
Comment #58
dawehnerThanks a lot for addressing those points!
Comment #60
gappleMoving back to RTBC since the re-test was successful.
Comment #63
gappleuh, not sure what happened there. Test page still shows passes...
Comment #64
xjmWhy are we adding this functionality to the menu block and not to the menu itself?
Comment #65
xjmI guess the answer to my question in #64 might be that menu blocks already allow configuring the menu levels in a way that the menu system itself doesn't (which as far as I can see only allows specifying that individual links be expanded or not), so it's not introducing a new pattern to configure the display of the menu on its block instance only.
Note that the screenshots have a redundant description that I was going to NW the patch for, but that is actually removed already in the current patch.
That said, though, I think this should go inside the "Menu levels" fieldset maybe?
Comment #66
swentel CreditAttribution: swentel commented@xjm There is an option for that actually. But maybe you might have two instances of that menu in a block where one is expanded, the other is not.
Comment #67
xjmI'd also recommend updated screenshots, since the current screenshots don't reflect the current patch. That way it's easier to ask for others to review the proposed change. Thanks!
Comment #68
xjmWhere is the option? I wasn't able to find it except on individual menu items. I was worried about things becoming confusing and people not seeing the results of their attempted changes, etc., but if the menu depth configuration is only on the block (with core anyway) then I convinced myself it makes sense for this option to be on the block as well.
Comment #69
swentel CreditAttribution: swentel commented@xjm yes, you're right, it's on the individual item only, sorry for any confusion :)
Comment #70
Sam152 CreditAttribution: Sam152 commentedMoved the new option into the menu levels fieldset. Also screenshots of how this works based on latest patch.
A menu with a top level item that is not expanded
How it appears before checking the expand checkbox
How the menu levels fieldset looks
What you see after you save
Comment #71
hkirsman CreditAttribution: hkirsman commentedIs it safe to use #56 for now because the latest failed (Drupal 8.2.2):
Comment #72
Sam152 CreditAttribution: Sam152 commentedI think the latest patch applies against 8.3.x. Anyone fancy another review of this? It was RTBC as of #63.
Comment #73
gmario CreditAttribution: gmario commentedHi,
I have correct the patch in the #56 to work with 8.1 (just the path of SystemMenuBlockTest.php file).
in attachment.
Comment #75
gappleThe failed test from #73 shouldn't have affected the issue status
Comment #76
gappleUpdated summary
Comment #77
Sam152 CreditAttribution: Sam152 commentedRerolled for 8.3.x.
Comment #78
dawehnerI'm wondering whether we should get the UX team involved. Is the label for itself self descriptive enough?
Comment #80
Sam152 CreditAttribution: Sam152 commentedMaybe a review can help move this forward.
Comment #81
yoroy CreditAttribution: yoroy at Roy Scholten commentedFrom the issue summary:
Is this for use cases like having all items available for making (mega menu) dropdowns in a main nav? So it's for "things like *some* primary navigation systems like mega menu dropdowns which require etc…" ? Would be useful to clarify the “Why” for this change a bit more.
Initially I had the same question as xjm in #64 about where this feature lives but that's ok (in a consistency sense, the current situation does not seem ideal but out of scope here)
As for the label, would it help to mention “always”?
“Always expand all menu items”
Comment #82
FeyP CreditAttribution: FeyP as a volunteer and at werk21 commentedWhen talking about possible use cases, one should note, that the patch adds an option to always expand all menu items per menu block, not per menu. So you can combine this with the existing options for initial menu level and maximum levels to display. This greatly increases the usefulness of this option. To give you a rough idea, we're currently using the patch for the following use cases on several different sites:
You could possibly come up with more use cases, if you put some thought into it.
If this setting was a per menu setting instead of a per menu block setting, a lot of our use cases wouldn't work anymore and the feature would be much less useful, if at all. We really need the flexibility to show the same menu with menu item settings honoured in one place and menu item settings overridden by this option in other places on the same site. But that's just us. Can't speak for the rest of the community of course ;).
Comment #83
esolitosFor those who need this on 8.2.x I backported the patch which was not applying anymore.
Comment #84
Wim LeersYes, this would be a menu block setting, not a
menu
setting.Comment #85
bircherRe-rolling the patch for 8.4.x
reverting also #2856625: Remove unused menu active trail service from SystemMenuBlock
We use this feature for the main navigation because we have a second level navigation block in the sidebar. And because in the sidebar we only want to have the menu tree of the active menu path we need to mark the menu items "not expand all". As such this should be a block configuration and not a menu configuration. Or at least be "overridable" on a per block basis.
The current custom workaround we use is to create a new "expanded_menu" block plugin.
Comment #86
ifrikI think adding a second location to configure whether menu items are expanded or not is only leading to confusion. We have an option to set a single menu item as expanded on the edit page for that item, and we need to keep that option anyway.
At the moment it is unclear how this tick-box for the instance of a menu relates to the general menu item settings.
And how does the user know that are two places for setting whether a menu item is expanded or not?
There are also cases where on one level the menu items should be expanded, but on the next one they shouldn't. Or where on one level some items are extended and others are not. This tick-box however seems to be an all-or-nothing solution.
There is another issue that's trying to reduce the amount of clicking needed to configure the display of a menu by adding a tick-box for Expanded on the Edit page of the menu instead of only on the menu item page. That will already improve the usability, without producing a potentially confusing duplication of configuration. #2802837: Improve discoverability of menu "expanded" option
Comment #87
Sam152 CreditAttribution: Sam152 commentedI don't think this is a duplicate feature. I'd appreciate input on how to improve the clarity of what this accomplishes given I feel like the use case has already been validated.
Comment #88
Sam152 CreditAttribution: Sam152 commentedAddendum: I seem to remember there being quite a bit of support for this, but scanning the issue can't see that many examples of people using it. Perhaps this belongs in contrib if not in core.
Comment #89
Fonski CreditAttribution: Fonski commentedThis patch is compatible with the 8.3.0 release and can be used with Composer. It is a reroll of the patch in #77, but this last works on the 8.3x-dev version but not with the current release.
Comment #90
yareckon CreditAttribution: yareckon for Ärzte ohne Grenzen e.V. / Médecins Sans Frontières commentedHey showing the whole tree regardless of active path is an important feature for any type of front-end menu manipulation.
The Menu items actually are one correct place to set this preference, but the output menu should have the option to override that preference down to the depth limit configured.
If folks can't wait for this to get into core, than the contrib menu_block module (https://www.drupal.org/project/menu_block) provides this in drupal 8. Still, this should definitely be a core option.
Comment #91
ifrikI'm not quite sure what the state of this feature request is, but if it's to be taken up then UI text need to be changed:
.
Comment #93
ckaotik@ifrik Adding explanation seems sensible. Just a side-note: This feature is not about whether or not a menu item is active/visible. That setting remains unaffected.
Here's an example of why the proposed feature is very useful, as it allows configurations like this setup:
Goals:
To achieve the first using the default behavior, we must mark all "Top" items as expanded. If we visit the page of "Top 1", the secondary navigation block will display all 5 "Sub" items, since their parents are all expanded. With the proposed patch, the menu items no longer need to be expanded, and instead the primary block can be set to "Expand all". The secondary block will then only display the three "Top" items and "Sub 1" + "Sub 2".
Comment #94
ifrikI'm just worried that users are confused if they don't know that there are two different places to set something and wonder why this one menu item is expanded even though they clearly said it shouldn't....
So we need a note on the edit form of the individual menu item, that the "Expanded" option can be overwritten as a whole when a menu is placed as a block.
And a second note on the block form, that this overwrites the individual menu item configuration.
And a new Use in the Help text: Expanding all menu items in a block.
Comment #95
ifrikSetting this to "needs work" to add the UI texts
Comment #96
Eric115 CreditAttribution: Eric115 at PreviousNext commentedRe-rolled the patch in #85 so it applies to the latest version of 8.4.x.
Comment #98
kim.pepperThis is broken in 8.5.x since #2856625: Remove unused menu active trail service from SystemMenuBlock https://www.drupal.org/node/2912736
Comment #99
juusechec CreditAttribution: juusechec as a volunteer commentedThis persists in drupal-8.5.0-rc1.
What files should I patch?
I've do the changes manually in the database:
Comment #100
juusechec CreditAttribution: juusechec as a volunteer commentedThis persists in drupal-8.5.0-rc1.
What files should I patch?
I've do the changes manually in the database:
Comment #101
dubcanada CreditAttribution: dubcanada commentedAttached patch for 8.5 to bring back MenuActiveTrail.
This probably needs to be rethought if we don't want MenuActiveTrail in system blocks?
Comment #102
Martijn de WitAdded some tests for #101.
Maybe the file name has to change from core-84x-... to core-85x- or core-86x-... :)
@dubcanada did you address and fix the problem commented in #98?
Comment #103
ricovandevin CreditAttribution: ricovandevin at Finlet for One Shoe commentedRe-rolled the patch against the current version of the 8.5.x branch. In https://cgit.drupalcode.org/drupal/commit/core/modules/menu_ui/src/Tests... the tests were (re)moved so the patch from #101 does not apply anymore in 8.5.4.
Please note:
Because of the above I'll leave the issue status to NW.
Comment #105
Sam152 CreditAttribution: Sam152 commented#93 is a great example of why this is useful, thanks for writing that. I'm adding that to the issue summary.
Adding a quick reroll for 8.7 which removes some of the old version specific comments. Can look at some of the failing tests as well as some some UI text requested by @ifrik in #94. I can't see any downsides of describing that these can be overriden in the two places these are configurable.
Comment #107
Sam152 CreditAttribution: Sam152 commentedSome test fixes. Not quite sure about the existing-config installer fails, asked in #2788777: Allow a site-specific profile to be installed from existing config how to go about fixing those.
Edit: also, since #2631468: Menu subtrees in menu blocks show all subitems regardless of the active menu item, some of the assertions we introduced while testing this are no longer valid, will need to update the kernel test accordingly.
Comment #108
Sam152 CreditAttribution: Sam152 commentedAddressing the test introduced in this issue. I removed all the irrelevant assertions and also switched the menu tree that was being used to test, since the one in the test case already had
'expanded' => TRUE
set, it wasn't actually proving that this was working.Comment #110
Sam152 CreditAttribution: Sam152 commentedRestoring both test cases that got removed in #103 and reuploading patch that got messed up in #108.
Comment #111
Sam152 CreditAttribution: Sam152 commentedComment #113
Sam152 CreditAttribution: Sam152 commentedAdding the additional help text requested by @ifrik.
Is this a significant enough feature to add to
hook_help
? I'm wondering what we'd say about this in there, it seems like it would be really out of context, given we don't document each individual configuration option and form element in there? Hoping the additional context added here is okay.Going to do a whole-pass over the patch again to review it, then hopefully any watchers who are interested in getting this over the line and do a review of it as well.
Comment #114
kim.pepperQuick review:
nit: the type hint should be above the variable assignment.
Why is this changed?
Comment #115
Sam152 CreditAttribution: Sam152 commented1. Good catch, can fix that up.
2. Good question, I asked about that here as well. It seems that we get test fails if any config in the tarballs aren't run through updb.
Comment #116
Sam152 CreditAttribution: Sam152 commentedAddressing feedback from #114. I also did a whole pass over the patch to simplify what I could and (hopefully) make the tests a lot clearer.
This looks ready to me! :)
Comment #117
kim.pepperComment #118
Sam152 CreditAttribution: Sam152 commentedIf anyone is interested in some manual testing, it would be great to update the screenshots in the issue summary. We've added some help text based on the UX review.
Comment #120
Sam152 CreditAttribution: Sam152 commentedAdding test coverage and fixing a bug reported to me by @pameela.
Comment #121
pameeela CreditAttribution: pameeela as a volunteer commented+1 for this feature. Passes manual testing.
Screenshot of menu with sublinks without the box checked:
Screenshot of menu with the box checked:
Screenshot of the UI with the checkbox:
Comment #122
acbramley CreditAttribution: acbramley at PreviousNext commentedThese titles/descriptions make sense to me and describe the functionality and options provided well.
Overall thorough patch and testing, double checked that all the existing block config was also updated too :)
Comment #124
Sam152 CreditAttribution: Sam152 commentedAdding screenshots taken by @pameeela to the issue summary. Crediting @acbramley for review and discussion at the Drupal South sprint.
Comment #125
alexpottIt would be good to test that the children aren't shown if the expand_all_items is then set to FALSE.
This should use the \Drupal\Core\Config\Entity\ConfigEntityUpdater for batched updates. Some sites have a lot of blocks.
Comment #126
Sam152 CreditAttribution: Sam152 commentedThanks for the review @alexpott. Fixed and fixed.
Comment #127
acbramley CreditAttribution: acbramley at PreviousNext commentedNice, feedback from #125 is fixed, back to RTBC!
Comment #128
alexpottI manually tested the initial level selection in conjunction with the new setting and everything worked as expected.
Issue credit:
Committed f335587 and pushed to 8.7.x. Thanks!
This can be even simpler!
We should also be asserting on the value.
Fixed on commit. I ran the MenuBlockPostUpdateTest locally first.
Comment #130
alexpott#94 was addressed in #113. Adjusting issue summary to reflect that.
Comment #131
Wim LeersGreat to see this is now done! :)
Comment #133
alexpottOops forgot to add my changes that I outlined in #128. Pushed a quick follow-up.
Comment #134
Sam152 CreditAttribution: Sam152 commentedWow, that post update hook is super simple, nice one! Thanks again for reviewing and following up :)
Comment #135
joelpittetI probably made a mistake, but
menu_block
recent change changed the Dependency Injection and constructor ended in there, this change broke the constructor. Is there a best practice to avoid accidentally doing this? Guessing "composition" ...Or should I make a follow-up to make this argument optional with fallback to
\Drupal::service()
?#2968049: Dependency Injection issue
Comment #136
joelpittetCreated a follow-up after discussing with @Berdir, @andypost and @gapple on slack. #3018863: Make SystemMenuBlock's new constructor argument dependency optional
Comment #137
jibranThis issue broke the upgrade test for DER 1.x to 2.x https://www.drupal.org/pift-ci-job/1147416.
I'm marking it NW so someone can confirm and then we can create the follow-up and mark this fixed.
Comment #138
jibranCreated #3020718: system_post_update_add_expand_all_items_key_in_system_menu_block fails if blocks module is disabled because attached patch fixed the DER upgrade test fail.