Problem/Motivation
If you add a menu link to the Administration menu, and that menu link has an unrouted URI, when the toolbar is oriented vertically, there is a Javascript error which prevents the buttons for child menus from appearing.
Steps to reproduce:
- Add menu link to administration menu at /admin/structure/menu/manage/admin/add
- Set menu link title to anything.
- Set link to any URL that is not routed in Drupal: (use https://www.google.com, for example)
- Set parent link to --Administration
- Save
- If toolbar is horizontal, click the arrow to change it to vertical
- Observe that buttons for child menus do not appear.
- Observe that error is logged in browser console.
Uncaught Error: Syntax error, unrecognized expression: #toolbar-link-https://www-google-com
at Function.se.error (jquery.min.js?v=3.4.1:2)
at se.tokenize (jquery.min.js?v=3.4.1:2)
at se.select (jquery.min.js?v=3.4.1:2)
at Function.se [as find] (jquery.min.js?v=3.4.1:2)
at k.fn.init.find (jquery.min.js?v=3.4.1:2)
at MenuVisualView.js?v=8.8.5:19
at Array.forEach (<anonymous>)
at n.render (MenuVisualView.js?v=8.8.5:18)
at _ (backbone.js:371)
at m (backbone.js:356)Proposed resolution
Issue appears to be in \Drupal\toolbar\Controller\ToolbarController::preRenderGetRenderedSubtrees(), where in this line:
$id = str_replace(['.', '<', '>'], ['-', '', ''], $url->isRouted() ? $url->getRouteName() : $url->getUri());
$url->getUri() contain unsafe characters for DOM IDs.
In toolbar_menu_navigation_links(), unrouted URIs are hashed like so:
$id = substr(Crypt::hashBase64($url->getUri()), 0, 16);
It seems to make sense to use that in the Controller pre-render method as well.
$id = str_replace(['.', '<', '>'], ['-', '', ''], $url->isRouted() ? $url->getRouteName() : substr(Crypt::hashBase64($url->getUri()), 0, 16));
Remaining tasks
Needs reviews and tests
User interface changes
N/A
API changes
N/A
Data model changes
N/A
Release notes snippet
(Major and critical issues should have a snippet that can be pulled into the release notes when a release is created that includes the fix)
| Comment | File | Size | Author |
|---|---|---|---|
| #26 | 3137154-toolbar-unrouted-link-6.patch | 1.04 KB | esod |
| #21 | broken child links.png | 42.09 KB | omkar.podey |
| #21 | breaks.png | 75.79 KB | omkar.podey |
| #21 | doesnot break.png | 73.43 KB | omkar.podey |
| #8 | BeforePatch_Externalink.png | 315.35 KB | priyanka.sahni |
Issue fork drupal-3137154
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
godotislateHere's a patch.
Comment #3
godotislateFailing test-only patch.
Comment #4
godotislateTest-only patch try 2.
Comment #5
godotislateFix with test.
Comment #6
godotislateComment #7
priyanka.sahni commentedComment #8
priyanka.sahni commentedI have tested by applying the patch#5.It was working fine and looks good to me.
Steps to test:
Add menu link to administration menu at /admin/structure/menu/manage/admin/add
Set menu link title to anything.
Set link to any URL that is not routed in Drupal: (use https://www.google.com, for example)
Set parent link to
Save
If toolbar is horizontal , click the arrow to change it to vertical
Observe that buttons for child menus gets appear.
Before_Patch_ExternalLink

Before_Patch_InternalLink

After_Patch_ExternalLink

After_Patch_InternalLink"

Comment #9
priyanka.sahni commentedComment #10
nod_Using base64 and substring will not work, with this code
http://www.google.comandhttp://www.google.com/somethingelsewill get the same ID.Try to look at
Html::getId()instead.Ok nevermind, spoke too soon
Comment #11
nod_Patch is ok, it fixes what goes into drupalSettings to match what is in the DOM.
But the test should probably check that the generated ID matches with the ID in drupalSettings since that was what the issue was.
Comment #12
nod_sorry for the emotional roller coaster.
Tests needs to be updated to check this more directly. We also need a comment that links what we're doing here to the
toolbar_menu_navigation_links()since that is where it comes from. In case this gets updated it needs to be updated in 2 places otherwise it'll break again.Comment #13
godotislateComment #20
smustgrave commentedTried replicating following the steps in the IS on 10.2 but wasn't able to trigger the error.
Just to confirm
Created a link in the Admin menu
Title is My test
Link is https://www.google.com/
Toolbar is vertical
Click save
Go horizontal no issue
Create another link
Toolbar is horizontal
Click save
Go vertical no issue.
Comment #21
omkar.podey commentedWhat I think is that the steps are incorrect maybe. As @smustgrave i got a similar result initially with the structure being

But after i changed the menu links parent to

--Administrationfor my test menu link(random link title).It did break the child links in the vertical toolbar.

I think this was the intended bug to be solved, right?
Comment #22
omkar.podey commentedComment #23
omkar.podey commentedComment #25
godotislate@omkar.podey: Yes, that's the issue. Thank you for the correction in the steps. It's been a couple years, and I'm off that project, but we saw the issue surface when we put a link to our pattern library in the admin menu for convenience, and QA caught that the menu broke in the vertical configuration.
Created an MR against 11.x-dev after tweaking the original patch to address feedback from #12. Not sure how to write a better test. The Javascript for the toolbar is more complicated than I can write a test for. It doesn't look like the original test captures the issue correctly anymore either. I've left the PR at draft to make the fix available against 11.x-dev for anyone who needs it. I'm willing to revisit the tests if anyone can provide some guidance on how to write them.
Comment #26
esod commentedReroll for Drupal 10.2.7. I don't have time to look at the test right now.
Comment #28
quietone commentedThe Toolbar Module was approved for removal in #3476882: [Policy] Move Toolbar module to contrib.
This is Postponed. The status is set according to two policies. The Remove a core extension and move it to a contributed project and the Extensions approved for removal policies.
The deprecation work is in #3484850: [meta] Tasks to deprecate Toolbar module and the removal work in #3488828: [meta] Tasks to remove Toolbar module.
Toolbar will be moved to a contributed project before Drupal 12.0.0 is released.
Comment #29
quietone commentedToolbar has moved to contrib