0

So I'm having a problem with dismissing a Ctools modal within a form submit's callback function. Once I have processed any submit data, I'd like to dismiss the modal without redirecting the page (easy enough), but each time I click the submit button with ctools_modal_command_dismiss() included in the callback function, I'm presented with an AJAX error such as below:

An AJAX HTTP error occurred. HTTP Result Code: 200 Debugging information follows.

I'm stumped as to why this is happening. The modal works perfectly without the dismiss function.

function pay_rise_menu() {
  $items = array();
  $items['pay_rise/%ctools_js'] = array(
    'page callback' => 'pay_rise_modal_email_share',
    'page arguments' => array(1),
    'access arguments' => array('access content'),
  );

  return $items;
}

function pay_rise_modal_email_share($ajax) {
  if ($ajax) {
    ctools_include('ajax');
    ctools_include('modal');

    $form_state = array(
      'ajax' => TRUE,
      'title' => t('Share by email'),
    );

    $output = ctools_modal_form_wrapper('pay_rise_modal_email_share_form', $form_state);

    if (!empty($form_state['ajax_commands'])) {
      $output = $form_state['ajax_commands'];
    }

    print ajax_render($output);
    drupal_exit();
  }
  else {
    return drupal_get_form('pay_rise_modal_email_share_form');
  }
}

function pay_rise_modal_email_share_form($form, &$form_state) {
  $form = array();

  $form['recipient'] = array(
    '#type' => 'textfield',
    '#title' => t('Recipient'),
    '#attributes' => array(
      'placeholder' => 'Recipient email...',
    ),
  );

  $form['body'] = array(
    '#type' => 'textfield',
    '#title' => t('Message'),
    '#attributes' => array(
      'placeholder' => 'Message...',
    ),
  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
    '#ajax' => array(
      'callback' => 'pay_rise_modal_email_share_form_callback',
    ),
  );

  return $form;
}

function pay_rise_modal_email_share_form_callback($form, &$form_state) {
  $values['message'] = $form_state['values']['body'];
  drupal_mail('pay_rise', 'share', $form_state['values']['recipient'], 'en', array('values' => $values));
  $form_state['ajax_commands'] = ctools_modal_command_dismiss('Thanks. Your message has been sent.');
}

The above is invoked once the following link is clicked:

l('email', 'pay_rise/nojs', array('attributes' => array('class' => 'ctools-use-modal icon-mail')))
6
  • " Debugging information follows." - post it. Look in watchdog, in server's logs etc.
    – Mołot
    Sep 5, 2014 at 14:32
  • That's the thing - it says 'error', yet there are no AJAX errors after checking logs/posting to dblog. I initially thought it might have something to do with execution_limit, however the response code is a 200. Sep 5, 2014 at 14:34
  • @AdamThomason Additionally you can see ajax request/response with Firebug extension(Console tab)
    – xurshid29
    Sep 5, 2014 at 15:23
  • I've tried firebug too, but with no luck! I'm beginning to think it's something down to my vagrant configuration, although surely this wouldn't only affect one AJAX request! Sep 5, 2014 at 15:28
  • @AdamThomason what it shows?
    – xurshid29
    Sep 5, 2014 at 15:42

2 Answers 2

0

You are doing it wrong, ctools modal works a little bit different. Better do it like this:

remove #ajax property from form submit button, let it work as usual:

...
$form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
  );
...

define submit callback as usual:

function pay_rise_modal_email_share_form_submit($form, &$form_state) {
  $values['message'] = $form_state['values']['body'];
  drupal_mail('pay_rise', 'share', $form_state['values']['recipient'], 'en', array('values' => $values));
}

build modal callback:

function myCallback($js = NULL) {
  if (!$js) {
    drupal_access_denied();
    drupal_exit();
  }

  ctools_include('modal');
  ctools_include('ajax');

  $form_state = array(
    'title' => t('My modal title'),
    'ajax' => TRUE,
    'build_info' => array(
      'args' => array(),
    ),
  );

  $commands = ctools_modal_form_wrapper('pay_rise_modal_email_share_form', $form_state);

  if (!empty($form_state['executed'])) {
    $commands = array();
    $commands[] = ctools_modal_command_dismiss();
  }

  print ajax_render($commands);
  drupal_exit();
}

ctools itself handles ajax submission.. That's it.

5
  • Hi @xurshid29. I notice that you've taken that example from a guide on how to do this, which I googled for before posting my question. The reason I have not used a standard submit handler is because of other things going on within the callback. The above above does not work either I'm afraid, and still generates the exact same AJAX error. Sep 5, 2014 at 18:20
  • @AdamThomason I've taken it from Ctools help directory:) and there are almost ten modal forms in my current project and they are all works by this way, they work fine, without any errors
    – xurshid29
    Sep 5, 2014 at 18:25
  • @AdamThomason And I also see that there are some missing keys in your $items array in hook_menu implementation..
    – xurshid29
    Sep 5, 2014 at 18:27
  • Just figured this out, see my answer. Thanks for all your help, much appreciated! Sep 5, 2014 at 18:29
  • The code which I sampled and put in here is not the complete module. I have only put in the code that is relevant to this issue, else there would be thousands of lines which would be unsightly. Thanks for your help. Sep 5, 2014 at 18:31
0

Managed to solve this myself with the following:

By default, the php.ini file on a precise64 vagrant box sets execution_limit to 30 (for some reason). Increasing this incrementally seems to have fixed the issue for me at least.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.