Problem/Motivation

Missing toggle button on other menus than Manage, cause an error JS : Uncaught TypeError: $orientationToggleButton[0] is undefined

Steps to reproduce

This can be reproduced by doing unset($items['administration']); for specific role or user and that using hook_toolbar_alter.

Proposed resolution

Two options :
1 - Add JS condition before calling $orientationToggleButton[0] on /core/modules/toolbar/js/views/ToolbarVisualView.js
2 - Bring back toggle button to all menus.

CommentFileSizeAuthor
#14 Before-Patch.png570.12 KBarunkumark
#14 After-Patch.png352.83 KBarunkumark

Issue fork drupal-3310075

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

redamakhchan created an issue. See original summary.

romainj’s picture

@redamakhchan are you sure it is related to the Admin Toolbar module? As the first proposed resolution it involves the core Toolbar.

redamakhchan’s picture

Project: Admin Toolbar » Drupal core
Version: 3.1.1 » 10.1.x-dev
Component: Code » ajax system
redamakhchan’s picture

@romainj you're right issue related to toolbar on the drupal core, not admin toolbar module. I changed project to drupal core.

catch’s picture

Title: unset Manage menu cause a js error because of toggle button being placed only on it. » Toolbar js errors when you remove the administration menu
Component: ajax system » toolbar.module
Priority: Critical » Normal

This looks like an issue introduced by custom code that we could make toolbar more resilient to, which isn't a critical bug.

sinn’s picture

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.

yookoala made their first commit to this issue’s fork.

yookoala changed the visibility of the branch 3310075-toolbar-js-errors to hidden.

yookoala’s picture

Status: Active » Needs review

The fix has implemented to both 10.4.x and 11.x branch. Please review.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

Issue summary mentions 2 solutions but no indication for which solution was chosen.

Not sure about the use case but if it seems like something people do will need test coverage I believe.

arunkumark’s picture

StatusFileSize
new352.83 KB
new570.12 KB

Verified the MR !7934 has fixed the Issue. Not changing the issue status until the approach is decided as per #13. Hopes the MR uses approach 2 to solve the issue for specific Menu types.

Before
Before

After
After

yookoala’s picture

I'd argue approach #1 and #2 are not mutually exclusive. While approach #2 is more of a user experience concern, approach #1 makes the code more resilient and robust either way.

pyrello’s picture

I think that approach #1 is appropriate to fix a JS error. Approach #2 seems like more of a feature request, even if it was previously a feature. Let's not let the possibility of a more robust feature block a simple JS fix for now.

@smustgrave Is it really necessary to add a test based on the addition of an if statement to check if a JS variable is set?

tce’s picture

I've run into this issue on a site where a restricted role uses the front-end theme. The toolbar loads, but .toolbar-loading is never removed, and the toolbar appears broken.

After debugging, I confirmed the crash happens in ToolbarVisualView.js when the orientation toggle button is missing:

$orientationToggleButton[0].value

Adding a simple guard resolves the issue cleanly:

// If the toggle button isn't present, exit early to avoid JS crash.
if (!$orientationToggleButton.length) {
  return;
}

This allows the rest of the toolbar JS to run, removes .toolbar-loading, and restores full toolbar visibility for the restricted role.

pyrello’s picture

Status: Needs work » Needs review

Setting back to needs review to see if we can get this fix in without adding a test.

ironnuts’s picture

#17 is helpful. If #17 is an accurate description of what happens (in all cases) where this issue occurs then test coverage should be simple. There is a FunctionalJavascript test in the toolbar core module named TestIntegration.php? (or similar). The last test function in that file tests the button toggling behaviour and appears relevant to this issue. It looks like we can add an assertion or two to test that the

.toolbar-loading

class described in #17 is removed as it seems (going by #17) it needs to be from the DOM. Testing for console errors would surely be a waste of time because such tests accomplish nothing. But if there is a discernible change to the DOM, however slight, so long as it happens consistently without the fix and the change to the DOM is consistently different as a result of the fix, then test coverage can and should be implemented as normal..

I looked at the Nighwatch tests but not sure that is the place to add new assertions. the FunctionalJavascript test looks like the best place.

ironnuts’s picture

Regarding #17 I do not see any changes to the DOM in the before and after screenshots nor signs of breakage to the UI? I know the screenshots were submitted by another user. Just wondering if the screenshots are based on a different manner of reproducing the issue? We need to be sure that the class is present then removed in a consistent way when this issue is reproduced. More step-by-step details on how to reproduce the error and if possible to incorporate #17 in the issue description would help so long as #17 is the right way to reproduce the bug.

Maybe we can get more screenshots showing the browser DOM inspector rather than the console and showing the class present before. the fix and missing after it?

ironnuts’s picture

BTW if #17 is an accurate reproduction of the bug/ issue then Proposed resultion no.1 is surely the right one. A follow-up issue can deal with no.2. It seems way outside the scope of this issue: especially if #17 is accurate and, as a result, we can provide test coverage for the bug.

smustgrave’s picture

Status: Needs review » Needs work

Seems consensus may be for test coverage. Also reading the summary again what's the scenario for calling unset($items['administration']); ?

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

Issue tags: -admin toolbar, -error js, -missing toggle on menus
quietone’s picture

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

Toolbar has moved to contrib