Drupal core will call attached behaviors passing two arguments: context and settings (which contains all the settings injected server side). Furthermore behaviors might be called not only after the DOM is loaded, but also in other scenarios like an AJAX request.
The first time that Drupal.attachBehaviors() is called, the context variable contains the document object representing the DOM, but for the rest of the calls context will hold the affected piece of HTML.
In a case where the JQuery UI Dialog library is loaded in the website through including "dialog.ajax.js" and "dialog.js" in a custom module, every time after the first time where the behavior is called (e.g. when a HTML snippet is added by an AJAX callback), the variable settings doesn't hold the same properties as for the DOM, being the property dialog one of the missing ones.
This leads to a Javascript error, since the Drupal behavior in "dialog.ajax.js" is expecting from settings to hold the missing property dialog.
So when we send an ajax response with two commands, one InsertCommand and one InvokeCommand, an error will be thrown after the InsertCommand and the InvokeCommand will not be executed.
The bug is at lines 38-44 in "dialog.ajax.js" under "core/misc/dialog/". Here Javascript is trying to get the element close from dialog of settings:
var originalClose = settings.dialog.close;
Since dialog is undefined (doesn't exist), you get the error:
Uncaught DrupalBehaviorError: attach ; dialog: Cannot read property 'close' of undefined
A solution would be to check before if settings.dialog is set, like this:
if (settings.hasOwnProperty(dialog)) {
// do something with settings.dialog
}
Comments
Comment #1
davidum commentedComment #2
davidum commentedComment #3
hchonovLets test it.
Comment #4
hchonovComment #5
hchonovComment #6
larowlanComment #7
mgiffordRe-uploading patch for the bots.
Comment #9
sean.walker@nreca.coop commentedI tested this out in Firebug console by attempting to run 3 Ajax commands at the same time. I received the same error messages described by mgifford before applying the patch for all Ajax calls after the first one. I then downloaded and applied the patch, tested again with the same 3 Ajax calls in the console and received no errors. This looks solid, so I am marking as reviewed by the community to keep moving this along. Please find screenshots of my experience attached.
Comment #10
alexpottWe now have JavascriptTestBase in core so javascript fixes should come with tests so we don't break it in the future.
Comment #13
dpagini commentedI decided to look into this issue as part of Drupalcon Baltimore 2017. I'm a first time sprinter at the Major Issue Triage table. I don't expect to finish this today, but my plan is to reproduce the issue locally and take a crack at a test. I will report back how far I can get.
Comment #14
dpagini commentedOk, I had some time to look into this tonight. I actually cannot replicate this problem any longer. I am running 8.4.x-dev, and will outline my test steps below. If I'm not following the right steps, please correct me if I'm wrong...
I have stood up a test site (standard install profile) and installed a custom module with a custom controller/form. My form puts a link and a button on the page. The link calls a modal (any admin page route in a modal) by way of the link render array class->use-ajax, data-dialog-type->modal. The button calls a custom ajax response to call the InsertCommand and an InvokeCommand. My InsertCommand adds a second link to the footer similar to the modal link just specified. The InvokeCommand just adds some text after each link. I also used a javascript "console.log" command to tell me every time the code in this patch (pre-patch code) was executed. It is executed on page load, and then again with each of the ajax commands above.
I'm not sure how else to reproduce this error. I have a suspicion that this issue may have been fixed by some other code updates so that it is no longer a problem.
@davidum, @hchonov et al - not exactly sure what to do with these findings at this point, but I hope this is helpful. If there's anything else I can do to investigate this issue, please let me know! I won't touch the status, but I assume "Closed (cannot reproduce)" is the appropriate status here?
Comment #15
droplet commentedTo remove (clean up all) the drupalSetting, isn't it a custom code mistake? I can imagine a lot of code will be broken if you empty the drupalSetting.
Comment #16
cilefen commentedI am updating credit for the triage work done by dpagini. Going forward, you should know that we like to see documented triage steps (even if brief). It is the only way to know if the triage has actually been completed. Here are some made-up examples of documented triage steps:
Thank you!
Comment #17
cilefen commentedWhoops - I forgot to actually check the box!
Comment #20
samuel.seide commentedI updated this patch to work for 8.4.x as this was an issue on a site I was working on.
Comment #22
samuel.seide commentedUpdate the patch to work for 8.6.x
Comment #23
eiriksmI just experienced this. Now, the patch may work for the problem, however, I feel it was a developer error.
The way I got the error was altering an AjaxResponse object with something like this:
I now realize the correct way (which works great) is something like this:
tldr: Stop using SettingsCommand :)
Comment #29
nod_See #23 :)
Comment #30
hchonovAnd see the issue description:
Looks different than #23 I think:
Comment #31
santhosh-kumar-rengasamy commentedThis same issue presents from the drupal version 9.2.x. Hence I made a patch for the version Dev-9.2.x.