Toolbar js errors when you remove the administration menu

Created on 16 September 2022, over 2 years ago
Updated 12 April 2023, about 2 years ago

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.

🐛 Bug report
Status

Active

Version

10.1

Component
Toolbar 

Last updated 7 days ago

  • Maintained by
  • 🇫🇷France @nod_
Created by

🇲🇦Morocco redamakhchan

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇬🇧United Kingdom catch

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

  • It might be a duplication of https://www.drupal.org/project/drupal/issues/3305152 🐛 Toggle icon for toolbar menu missing for all tabs except Manage Needs work

  • First commit to issue fork.
  • 🇭🇰Hong Kong yookoala

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

  • Pipeline finished with Success
    about 1 year ago
    #165965
  • Pipeline finished with Success
    about 1 year ago
    Total: 624s
    #165967
  • Status changed to Needs review about 1 year ago
  • 🇭🇰Hong Kong yookoala

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

  • Status changed to Needs work about 1 year ago
  • 🇺🇸United States smustgrave

    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.

  • 🇮🇳India arunkumark Coimbatore

    Verified the MR !7934 has fixed the Issue. Not changing the issue status until the approach is decided as per #13.

    Before

    After

  • 🇭🇰Hong Kong yookoala

    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.

  • 🇺🇸United States pyrello

    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?

  • 🇬🇧United Kingdom tce

    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.

Production build 0.71.5 2024