Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
The menus are sorted by the machine name and not by the label so when you create a menu named "Axe" with machine name like "zzz" this item will be placed at the end of the list when you expect it to be at the top od the elements as the name start with "A".
Proposed resolution
Order the menus by the label and not for the machine name.
Remaining tasks
Check if this behavior is present in other menu elements.
User interface changes
The menu elements will be sorted the labels and not by machine name.
API changes
None.
Data model changes
None.
Release notes snippet
None.
Comment | File | Size | Author |
---|---|---|---|
#10 | admin-toolbar-menu-weight-3062573-10.patch | 7.08 KB | oknate |
| |||
#10 | interdiff.txt | 826 bytes | oknate |
Comments
Comment #2
oknateHere's an initial patch.
Comment #3
oknateAdding test coverage, although I'm having trouble with it NOT failing. I know I saw this bug locally, so I need to rework the test to actually demonstrate the problem.
Comment #4
oknateI forgot that the paths are prefaces with /subdirectory/ on testbot:
This fixes failures like this:
Of course, this was my test, case that was supposed to fail, so back to work on this.
Comment #5
oknatepatch #2 has unexpected results testing locally on Drupal 8.8:
trying a test like this:
without the patch:
with the patch:
so, it seems the patch isn't a complete solution, although it does fix the order of the extra links, the original links are still first.
I think because DefaultMenuLinkTreeManipulators::generateIndexAndSort uses weight as well as label:
when I test on my other local site (8.7.1), it's doing what I expect.
I'm still investigating why.
Comment #6
oknateI think I've figured out the problem.
This loads the menu ids keyed by id.
$menu_ids = $this->entityTypeManager->getStorage('menu')->getQuery()->pager(self::MAX_BUNDLE_NUMBER)->execute();
I was trying to add
->sort('label')
but that didn't work. I guess because they're config entities? It didn't throw an error, but it didn't give accurate results.
changing it to
->sort('title')
seemed to help, but it didn't work on Drupal 8.8. It was throwing an error. Plus, I don't think that will work when the menu is translated. I think this might still need some work display translated titles sorted correctly. It turns out Menu::sort is actually a method used on that page! It's actually ConfigEntityBase::sort().
I ended up fixing it by looking how ConfigEntityListBuilder::load sorts them, because /admin/structure/menu has them sorted correctly.
Unfortunately, this means we need to load all of the entities to get this accurate. Perhaps we can come up with a different way later, but at least this works.
Comment #8
oknateOops. I got tripped up by testbot prefacing the urls with '/subdirectory/' again.
Comment #10
oknateRenaming the test so as not to conflict with other issue I'm working on, 3063356.
Comment #11
oknateI tried to test this with translation. But as far as I can tell, menus don't translate, they have a set language, and all menus in all languages display.
Comment #13
adriancidthanks
Comment #15
pawel_r CreditAttribution: pawel_r commentedIs there any possibility not to use new way of sorting? Of course beside reverting changes from commit?
Comment #16
adriancidWhat kind of sort do you want to use?
Comment #17
pawel_r CreditAttribution: pawel_r commented@adriancid Exactly the same it was before (if there was any at all). Can upgrade to 2.x branch only with condition: sequences in all menus remains the same.