The $toolbar variable is declared outside the scope of Drupal.behaviors.toolbar. The value is assigned in this line of code.

$toolbar = $(context).find('#toolbar-administration').once('toolbar');

On the first pass through attaching behaviors, $toolbar gets assigned a jQuery object with one element. On any subsequent pass on the save page, such as after an Ajax request, $toolbar is set to an empty jQuery object because the .once() method will on select anything on a second pass.

Screenshot the Chrome development tools showing the $toolbar variable is assigned an empty array after an AJAX call on the page

We need to update the code so that $toolbar is assigned only on the first pass through the attaching of behaviors.

Comments

David_Rothstein’s picture

Priority: Normal » Critical
Status: Active » Needs review
StatusFileSize
new827 bytes

Marking #1872274: Editing breaks the toolbar as duplicate and moving to critical, since this really causes the toolbar to break.

Simple way to reproduce is to go to admin/structure/views/add and change the view from Content to Users - that kicks off an Ajax request which triggers this.

Here's a patch along the lines of above. Somehow the whole code flow seems a little odd, but I guess it's OK (since $toolbar is a global variable we must be assuming that there is only one of them on the page, but we still look for it again every Ajax request on the off chance that the Ajax request caused the existing toolbar to be destroyed and a new one to be added?)

David_Rothstein’s picture

Component: other » toolbar.module
damien tournoud’s picture

Status: Needs review » Reviewed & tested by the community

The patch makes sense. Let's get this in.

jessebeach’s picture

StatusFileSize
new1.41 KB

I'd like to change the wording just a tad in the comment in this patch. In this case $toolbar is not a global variable; it is referenced by the closure of the function Drupal.behaviors.toolbar.attach and $toolbar is scoped to the anonymous immediately invoked function expression that surrounds all of the code in this file. It's true that this code assumes one toolbar on the page, which is why it is selected by an id in $(context).find('#toolbar-administration').once('toolbar'), not a class. This patch doesn't change the behavior of the fix, just the name of the $newToolbar variable to $onceCheck and adjusts the comment accordingly.

webchick’s picture

Status: Reviewed & tested by the community » Fixed

Committed and pushed to 8.x. Thanks!

David_Rothstein’s picture

Priority: Critical » Normal
Status: Fixed » Needs work

Can we go back to something more like the original variable name? The thing that actually gets stored in this variable is a toolbar object, so calling it "onceCheck" doesn't sound right. I mean, lines like this just look really odd to me:

$toolbar = $onceCheck;

You're right about the code comment, though. It isn't a global variable (I forgot about the mega-function that wraps everything in Drupal JavaScript).

David_Rothstein’s picture

Status: Needs work » Needs review
StatusFileSize
new941 bytes

Something like this, perhaps. I couldn't come up with anything better than $newToolbar, but pretty much anything that has the word "toolbar" in it works for me.

jessebeach’s picture

The only reason that the $onceCheck variable exists is to check to see if once() has run on the toolbar.

What about $toolbarOnce?

David_Rothstein’s picture

Sure, why not.

jessebeach’s picture

StatusFileSize
new1.43 KB

Changed $onceCheck to $toolbarOnce.

Status: Needs review » Needs work
Issue tags: -js-novice, -toolbar-followup

The last submitted patch, 1858576_toolbar-var_10.patch, failed testing.

wim leers’s picture

Status: Needs work » Needs review
Issue tags: +js-novice, +toolbar-followup

#10: 1858576_toolbar-var_10.patch queued for re-testing.

David_Rothstein’s picture

StatusFileSize
new942 bytes

Fixed the code comment also.

tstoeckler’s picture

Status: Needs review » Reviewed & tested by the community

That definitely makes it more readable. Great!

webchick’s picture

Status: Reviewed & tested by the community » Fixed

Committed and pushed to 8.x. Thanks!

Automatically closed -- issue fixed for 2 weeks with no activity.