While debugging a reopened/repurposed issue in the Wysiwyg module - see #1757684-25: Ajax error on Add another item (#25 and onwards) - I noticed that Drupal (7) will automatically call Drupal.attachBehaviors(this.form, settings) for any form submitted through AJAX, such as a node edit form in a CTools Modal. The problem is, the modal is closed and the form has been removed from the document, only the reference to it in the AJAX instance is keeping it around.

This can cause problems for behaviors trying to work with the DOM elements in the form, since they can't reach outside the form, and elements in the form can't be reached from anywhere else in the document. On top of that, since closing the modal does not generate any settings commands, Drupal defaults to passing in all of Drupal.settings for just the form element. Reality gets out of sync with the script states as the form element is whisked away into nothingness as soon as the AJAX operation is over.

How about removing the reference keeping the form around and work around Core getting us into this mess?
The attached patch does this quite easily, but I'll also post report the problem in Core, so that should show up as a related issue soon.

Btw, I'm hesitant to call this a bug in CTools, since I think Core shouldn't behave this way in the first place, but I wasn't sure what to call it.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

tim.plunkett’s picture

TwoD’s picture

If #2215857: Behaviors get attached to removed forms gets in, this patch isn't technically needed anymore, but having it anyway wouldn't hurt since there is still a slight risk some other snippet of code in another module does something similar when the form appears to still be there. It does make it a lot more obvious from an external POV that the form in the modal was just obliterated.

Chris Matthews’s picture

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

The patch to modal.js does not apply to the latest ctools 7.x-1.x-dev so needs a reroll.

Checking patch js/modal.js...
error: while searching for:
    $('body').unbind('keypress', modalEventHandler);
    $('.close').unbind('click', modalContentClose);
    $(document).trigger('CToolsDetachBehaviors', $('#modalContent'));

    // jQuery magic loop through the instances and run the animations or removal.
    content.each(function(){

error: patch failed: js/modal.js:516
error: js/modal.js: patch does not apply
Andrew Answer’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
FileSize
569 bytes

Patch rerolled.