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:

  1. Add menu link to administration menu at /admin/structure/menu/manage/admin/add
  2. Set menu link title to anything.
  3. Set link to any URL that is not routed in Drupal: (use https://www.google.com, for example)
  4. Set parent link to --Administration
  5. Save
  6. If toolbar is horizontal, click the arrow to change it to vertical
  7. Observe that buttons for child menus do not appear.
  8. 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)

Issue fork drupal-3137154

Command icon 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

godotislate created an issue. See original summary.

godotislate’s picture

Issue tags: +Needs tests
StatusFileSize
new1.03 KB

Here's a patch.

godotislate’s picture

Failing test-only patch.

godotislate’s picture

Test-only patch try 2.

godotislate’s picture

Status: Active » Needs review
StatusFileSize
new3.03 KB
new920 bytes

Fix with test.

godotislate’s picture

Issue tags: -Needs tests
priyanka.sahni’s picture

Assigned: Unassigned » priyanka.sahni
priyanka.sahni’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new391.38 KB
new349.9 KB
new343.42 KB
new315.35 KB

I 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_ExternalLink

Before_Patch_InternalLink
Before_Patch_InternalLink

After_Patch_ExternalLink
After_Patch_ExternalLink

After_Patch_InternalLink"
After_Patch_InternalLink

priyanka.sahni’s picture

Assigned: priyanka.sahni » Unassigned
nod_’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/modules/toolbar/src/Controller/ToolbarController.php
@@ -121,7 +122,7 @@ public static function preRenderGetRenderedSubtrees(array $data) {
+      $id = str_replace(['.', '<', '>'], ['-', '', ''], $url->isRouted() ? $url->getRouteName() : substr(Crypt::hashBase64($url->getUri()), 0, 16));


Using base64 and substring will not work, with this code http://www.google.com and http://www.google.com/somethingelse will get the same ID.


Try to look at Html::getId() instead.

Ok nevermind, spoke too soon

nod_’s picture

Status: Needs work » Reviewed & tested by the community

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.

nod_’s picture

Status: Reviewed & tested by the community » Needs work

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.

godotislate’s picture

Issue summary: View changes

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Status: Needs work » Postponed (maintainer needs more info)

Tried 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.

omkar.podey’s picture

Issue summary: View changes
StatusFileSize
new73.43 KB
new75.79 KB
new42.09 KB

What 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 --Administration for 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?

omkar.podey’s picture

Status: Postponed (maintainer needs more info) » Needs work
omkar.podey’s picture

Issue summary: View changes

godotislate’s picture

@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.

esod’s picture

StatusFileSize
new1.04 KB

Reroll for Drupal 10.2.7. I don't have time to look at the test right now.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

quietone’s picture

Status: Needs work » Postponed

The 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.

quietone’s picture

Project: Drupal core » Toolbar
Version: main » 1.x-dev
Component: toolbar.module » Code
Status: Postponed » Needs work

Toolbar has moved to contrib