Drupal Ctools Modal Handling

1.8k Views Asked by At

I'm calling a function with URL http://mywebsite.org/param/ajax?ID=2 which will return a form in html for which i use the following functions in the corresponfin callback function in my custom module.

$form_state = array(
  'ajax' => TRUE,
  'title' => t("Edit : $ID"),
);
$output = ctools_modal_form_wrapper('form_fn', $form_state);
print ajax_render($output);
drupal_exit();

I use this output to create a form inside a twitter bootstrap modal. The problem is when I'm submitting I get an ajax output again which I'm not able to handle. I would like to show a success message or failure based on the form_submit function. Is there any way I can show the output using the modal or on the original page which called the modal.

2

There are 2 best solutions below

0
On

call to ctools modal should look like this:

  $output = ctools_modal_form_wrapper($form_name, $form_state);
  if(!empty($form_state['executed'])) {
    $output = array();
    $output[] = ctools_modal_command_display(t('Successful message title'), 'Successful message body');
    $output[] = ctools_ajax_command_reload();
  }
  print ajax_render($output);
  exit;

Don't forget to check for $form_state['executed'].

Also if you use a ctools_ajax_command_reload() this will reload a content of a modal and should fix the problem with another ajax output.

0
On

Would upvote sirBlond but my reputation isn't up there yet. I used this in the case where my modal window was not closing in IE8 on successful form submission. I needed to add this right before my redirect.

$commands[] = ctools_ajax_command_reload()     
$commands[] = ctools_ajax_command_redirect($path, 0, $options);

Was exactly what I needed. Thanks.