Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I have a menu item with page callback:

function my_menu() {
  $items = array();
  $items['my/action/%ctools_js'] = array(
    'title' => 'do something',
    'page callback' => 'my_action',
    'page arguments' => array(2,3),
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );
}

function my_action($js = NULL, $nid = NULL) {
  $form_state = array('nid' => $nid);
  if(!$js) {
    drupal_build_form('my_action_form', $form_state);
  }
  ctools_include('modal');
  ctools_include('ajax');
  $form_state['ajax'] = $js;
  $form_state['title'] = t('Form Title');
  $output = drupal_build_form('my_action_form', $form_state);
  $commands = array();
  if (!empty($form_state['finished'])) {
    $commands[] = ctools_modal_command_dismiss();
    $commands[] = ctools_ajax_command_redirect('challenges');
  } else {
    ctools_modal_form_render($form_state, $output);
  }
  print ajax_render($commands);
  exit;
}

function my_action_form($form, &$form_state) {
  // form fields defined here
}

function my_action_form_validate($form, &$form_state) {
  // $form_state['finished'] = true;
}

function my_action_form_submit($form, &$form_state) {
  $form_state['finished'] = true;
}

If I uncomment out the line in my_action_form_validate The modal form dismisses and the redirect works. But with that line commented out when I try to set the $form_state var in submit, the modal doesn't dismiss and doesn't redirect. The form submit code executes (calls to drupal_set_message work fine), but by the time it gets back to the conditional (line 11) in my_action, $form_state has been set to default and a blank form reappears in the modal popup. How can I dismiss the modal window after submission?

share|improve this question
add comment

1 Answer 1

The issue was in the callback frunction my_action(). I was using the ctools_modal_form_render() function wrong. In the end I replaced the call to ctools_modal_form_render() with ctools_modal_form_wrapper():

function my_action($js = NULL, $nid = NULL) {
  if(!$js) {
    $form_state = array( 'nid' => $nid );
    return drupal_build_form('my_action_form', $form_state);
  }
  ctools_include('modal');
  ctools_include('ajax');
  $form_state = array(
    'title' => t('My Action Form'),
    'ajax' => TRUE,
    'nid' => $nid
  );
  $output = ctools_modal_form_wrapper('my_action_form', $form_state);
  if ( !empty($form_state['executed']) ) {
    $output = array();
    $output[] = ctools_modal_command_dismiss();
  }
  print ajax_render($output);
  exit;
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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