I've seen a few examples, but I cannot get this to work. I setup a similar modal using ctools as the example provided in 'Chaos Tools AJAX Demo' - with the custom modal wizard. After submitting and dismissing the modal, I want to redirect the page, but it's not working. I've added $commands[] = ctools_ajax_command_redirect('my-module-path'); to the example module...

    if ($output === FALSE || !empty($form_state['complete'])) {
      // Dismiss the modal.
      $commands[] = ajax_command_html('#ctools-sample', $animal);
      $commands[] = ctools_modal_command_dismiss();
      $commands[] = ctools_ajax_command_redirect('my-module-path');

But after the modal closes, the page remains. I've also tried ctools_ajax_command_reload(), but it wasn't reloading the page. Am I missing something to get this to work?

Thanks,
Tony

Comments

daffie’s picture

Hi Tony,

I had the same problem. :(
The solution is that you must add the javascript file ajax-responder.js.
This file does the execution of the reload and redirect commands. And a lot more.
Add this line to your code.

  ctools_add_js('ajax-responder');

You must place this line before declaring your ajax link to your modal form.
In the ctools_ajax_sample module you place the line in the function ctools_ajax_sample_page().

If this does not work for you, please let me know.

Tony Stratton’s picture

That worked! Thank you very much.

Tony Stratton’s picture

Status: Active » Closed (fixed)

...closing issue...

nagiek’s picture

Thanks this helped me too. I just figured all the necessary stuff would go into ctools_modal_add_js.

romaingar’s picture

thanks,
This is very useful even for ctools_modal_command_dismiss();

ctools_add_js('ajax-responder','ajax-responder');

parijat.chauhan’s picture

I face similar problem and not able to find why reload is not working

function mainlist_slot($account, $type, $mid, $js, $species, $step = NULL) {
  $types_having_cage = array('bird', 'mammal');

  if ($js) {
    ctools_include('modal');
    ctools_include('ajax');
    ctools_add_js('ajax-responder');
  }

  $list_url = sprintf("user/%d/categories/%s/list/%d/", $account->uid, check_plain($type), $mid);

  $form_info = array(
    'id' => 'mainlist_slot',
    'path' => $list_url . ($js ? 'ajax' : 'nojs') . "/$species/%step",
    'show trail' => FALSE,
    'ajax' => $js,
    'show back' => TRUE,
    'show cancel' => TRUE,
    'show return' => FALSE,
    'back callback' => 'mainlist_slot_wizard_back',
    'next callback' => 'mainlist_slot_wizard_next',
    'finish callback' => 'mainlist_slot_wizard_finish',
    'cancel callback' => 'mainlist_slot_wizard_cancel',
    'order' => array(
      'options' => t('Choose option'),
      'upload' => t('Upload image'),
      'details' => t('Details'),
    ),
    'forms' => array(
      'options' => array(
        'title' => t('Choose option'),
        'form id' => 'mainlist_slot_options_form',
      ),
      'upload' => array(
        'title' => t('Upload photo'),
        'form id ' => 'mainlist_slot_upload_form',
      ),
      'details' => array(
        'title' => t('Provide details'),
        'form id' => 'mainlist_slot_details_form',
      ),
    ),
  );

  // build unique object cache key
  $slot_id = mainlist_slot_build_object_cache_key($account->uid, $mid, $type, $species);
  if (empty($step)) {
    slot_object_cache_clear($slot_id);
    // We reset the form when $step is NULL because that means they have
    // for whatever reason started over.
    $step = 'options';
  }


  // This automatically gets defaults if there wasn't anything saved.
  $slot = slot_object_cache_get($slot_id);
  //if no slot was retrieved from cache create a slot by assmbling data
  if (empty($slot)) {
    $slot = new stdClass();
    $slot->uid = $account->uid;
  }

  if (empty($slot->mid)) {
    $slot->mid = $mid;
  }

  if (empty($slot->species)) {
    $slot->species = $species;
  }

  if (empty($slot->type)) {
    $slot->type = $type;
  }

  /*
    TODO
    - we should be able to fetch this information, muid, cid etc from url or post data ?
  */


  if (empty($slot->muid)) {
    $muid = _get_muid_from_user_mid($account->uid, $mid);
    $slot->muid = $muid;
  }

  if (empty($slot->msid)) {
    $mainlist_slot = mainlist_slot_get_slot($slot->species, $slot->muid);
    $slot->msid = $mainlist_slot->id;
  }


  if (in_array($type, $types_having_cage) && empty($slot->cid)) {
    $cage = get_cage($species, $account->uid);
    $slot->cid = $cage->cid;
  }

  $form_state = array(
    'ajax' => $js,
    'slot_id' => $slot_id,
    'slot' => & $slot,
  );

  if ($js) {
    $form_state['modal'] = TRUE;
  }

  ctools_include('wizard');

  $output = ctools_wizard_multistep_form($form_info, $step, $form_state);

  if ($js) {
    $commands = array();
    if ($output === FALSE || !empty($form_state['complete'])) {
      $commands[] = ctools_ajax_command_reload();
      $commands[] = ctools_modal_command_dismiss();
    }
    elseif (!empty($form_state['cancel'])) {
        $commands[] = ctools_modal_command_dismiss();
    }
    else {
      $commands[] = ctools_modal_form_render($form_state, $output);
    }
    print ctools_ajax_render($commands);
  }
  else {
    if ($output === FALSE || !empty($form_state['complete'])) {
      return drupal_goto($list_url);
    }
    elseif (!empty($form_state['cancel'])) {
      return drupal_goto($list_url);
    }
    else {
      return $output;
    }
  }
}