Drupal 7: PopUp By Ctools Automodal Module

  • Ctools Automodal will help you to create popup in your site. It is an easiest way to create a popup without any burden. To Create a popup form please follow the below instruction:-

  • Download and install the ctools Automodal(module) in your drupal instance.

    • Description : By using this module You can create your own custom form in popup.

  • Create menu callback for the popup form like below and create custom form for the callback function.

    • In your menu callback add  ‘modal' => TRUE, to support automatic modal.

      • In your hook_menu() function add "'modal' => TRUE" like below:

$items['abc/def'] = array(
   'title' => 'Title Text',
   'description' => 'Title Text',
   'page callback' => 'drupal_get_form',
   'page arguments' => array('callback_function'),
   'type' => MENU_CALLBACK,
   'access callback' => TRUE,
   'modal' => TRUE,  // This is the line where magic happens
 );
  • Write Your Form in Your menu callback function which you want in popup  

function callback_function($form, &$form_state) {
   $form['sample'] = array(
       '#type' =>’ textfield’,
       '#title' => 'Sample Field',
       '#required' => TRUE,
   );
   $form['submit'] = array(
     '#type' => 'submit',
     '#value' => t('Submit'),
   );
   return $form;
}

 

  • Write piece of code in your callback submit function.

    • In this code, your are redirecting the user after has been submitted by ajax.

    • In this function. We have to include ajax and one more js file to submit the form by ajax.

    • Here you have to pass redirect path in ctools_ajax_command_redirect() function, then form will redirect to next page after form has submitted.

    • Exit is to stop the ajax execution.

// submit function of your form
 function callback_function_submit($form, &$form_state) {
       $redirect = 'redirect/path'; // define redirect path
       $commands = array();
       ctools_include('ajax');
       ctools_add_js('ajax-responder');
       $commands[] = ctools_ajax_command_redirect($redirect); // To redirect after form submit.
       print ajax_render($commands);
       exit;
 }

 

  • Use “ctools-use-modal” class for the link. For which link  user should get popup as below:

    •  l('Test', 'abc/def', array('attributes'=>array('class'=> array('ctools-use-modal'))));

 

Category: