Problem/Motivation

I have a delete confirmation form that is "AJAX-ified". But the AJAX functionalities stop working when the form is displayed via jQuery dialog.

Below is the relevant code that "AJAX-ifies" the confirmation form.

  // Add an AJAX callback.
  $form['actions']['submit']['#ajax'] = array(
    'callback' => 'lq_comment_lq_comment_comment_delete_form_submit_ajax',
    'wrapper' => $form['#id'],
  );

When viewed normally, the AJAX callback is executed and the form is submitted via AJAX. But when viewed from the jQuery dialog, the form is submitted and the AJAX callback is not executed.

I tried adding ajax.js manually to ensure that the JS file is included via:

  $form['ajax_file'] = array(
    '#markup' => '<script src="<path to>/misc/ajax.js"></script>',
  );

When viewed normally, I can see the line added inside the form but when viewed via jQuery dialog, it seems that the line is stripped.

Is there a way to maintain AJAX functionalities of forms displayed via jQuery dialog?

Thanks!

Comments

drclaw’s picture

Status: Active » Closed (works as designed)

Here's what's happening:

  • Your AJAX form, when rendered, outputs it's AJAX javascript settings into Drupal.settings, but only on the page that it's originally rendered.
  • When simple dialog loads your form, it doesn't render it. It just pulls the HTML off the page it lives on. So, since your form wasn't rendered on the page you're currently on, your javascript settings aren't there and your AJAX doesn't work.

This is a bit of a tricky one to work around actually. I have two recommendations, but they both require some custom coding. The easiest solution would be to actually add your form to the page (in a block, for example). Then, hide it using css. Then just add your own javascript to show/hide the form in a dialog:

// This turns your block into a jQuery dialog
$('#my-block-wrapper-id').dialog({
  autoOpen: false,
  modal: true,
  // ... other options
});
// This adds a click handler to your link to launch the dialog
$('a.this-link-launches-dialog').once('myDialog').click(function (event) {
  // Prevent the default link behavior
  event.preventDefault();
  // Launch the dialog
  $('#my-block-wrapper-id').dialog('open');
});

The second option would be to use ctools. Ctools has some cool stuff that helps power AJAX loading of forms as well as modal dialogs. Check out Modal Forms for an example of how it can work.

Hope this helps!
drclaw

angheloko’s picture

Ah, gotcha! Will try them out.

And, I guess this is actually outside of the modules main purpose anyway and the module does what it does best.

Thanks for the looking into this and for the suggestions!