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?