Thank you for this easy to use module. I'd like to a for a way to auto close the modal on user submition if the form doesn't have errors. Can you please provide some advise?

I have search in the web and I get to the ctools_modal_command_dismiss() function but I'm not sure where to put it.

Thank you



julianmancera’s picture


I have found a solution, it needs this code change in the ctools_automodal.module line 127 function ctools_automodal_get_form:

$commands = ctools_modal_form_wrapper($form_id, $form_state);
    if ($form_state['executed']) {
      $commands = array(ctools_modal_command_dismiss());
    }else {
	    if (empty($commands)) {
	      $commands[] = ctools_modal_command_loading();
	      if (!empty($_GET['destination'])) {
	        $commands[] = ctools_ajax_command_redirect($_GET['destination']);

Please let me know you comments. The other posibility is in the submition function for the form add a form_state['command'] array variable that allows to control wether to close the modal or maintain the user there for further user operations.

Let me know your comments

Julian Mancera

HakS’s picture

Status: Active » Needs work

Hi julianmancera

Unfortunately it does not work for me, form still stays open when submit after applying changes.

I tested it putting this code inside an implementation of hook_menu_alter:

$items['node/add/article']['modal'] = TRUE;

That's the core node add form.

I was trying it on an (almost) clean installation.

HakS’s picture

Actually, it seems to work if form is not an administrative form. I created a custom form and it worked, but I don't know if that would be part of this issue, because I noticed that administrative forms on modal windows behave quite strange.

EDIT: If you allow other users to use that modal administrative form (in my case that means allowing anonymous to add articles), when submit an ajax error happens http://d.pr/i/IjA9

Marko B’s picture

Issue summary: View changes

There is no need for that, you are using it wrong You can do it all with form rebuild and ctools_modal_command_dismiss(); and ajax_render, I also added $commands[] = ctools_ajax_command_redirect(''); so you redirect when you submited a form to a page you want.

function custom_form($form, &$form_state)
 $form = array();
  $form['something'] = array(
    '#type' => 'textfield',
    '#title' => t('Enter'),
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
 if ( $form_state['submitted']){
        $commands[] = ctools_modal_command_dismiss();
         $commands[] = ctools_ajax_command_redirect('<front>');
    print ajax_render($commands);
  return $form;

function custom_form_submit(&$form, &$form_state) {

  $form_state['rebuild'] = TRUE;

Marko B’s picture

Status: Needs work » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

capellic’s picture

@MarkoB, thanks for your code, however I had to add these two module_load_include() calls in order for the ctools_modal_command_dismiss() and ctools_ajax_command_redirect() to be found:

module_load_include('inc', 'ctools', 'includes/modal');
module_load_include('inc', 'ctools', 'includes/ajax');
sukh.singh’s picture

@capellica, the proper way to add these files are as follow


add this to the function which you are using against the ctools ajax menu link

ongraph’s picture

You can put ctools_modal_command_dismiss(); after successful form submission
Here is coding example of ctools modal auto close with form submission

function custom_module_form($js = NULL) {
// Fall back if $js is not set.
if (!$js) {
return drupal_get_form('custom_form');

$form_state = array(
'title' => t('Modal example'),
'ajax' => TRUE,
$output = ctools_modal_form_wrapper('custom_form', $form_state);
if (!empty($form_state['executed'])) {
$output[] = ctools_modal_command_dismiss(); //Close modal automatically
$output[] = ctools_ajax_command_reload(); //reload parent window
print ajax_render($output);

ugintl’s picture

The code from #1 is working, but the page is not updated with the changes automatically. I have to refresh the page to make the changes.