Form API and Ajax frameworks work quite well together.
In your submit button, that you want to trigger the popup, it should work as an Ajax element first.
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit (and open popup)',
);
If you use submit and validate handlers as you have shown in your question, those submit and validate handlers will be executed at this point. If you want different submit/validate handlers, then you have to add them to the submit button itself.
Now, add #ajax
property to the element.
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit (and open popup)',
'#ajax' => array(
'wrapper' => 'id_of_some_unique_element',
'callback' => 'my_module_give_this_function_a_nice_name',
),
);
ctools_include('modal');
ctools_modal_add_js();
We play a trick here. Typically, #ajax
property is used to replace some parts of the form. That's why we use the wrapper to indicate the replacing element. This will not be used at all if you trigger the modal, but make sure you refer to a unique DIV. Better leave it empty.
The callback
is what we need. This callback should be a PHP function name, that returns some or full $form
first parameter, or, it can print some Ajax commands. Do not set $form_state['rebuild'] = TRUE
in your #submit
handler.
ctools_add_modal_js()
adds the modal.js file because it's necessary to trigger the popup.
function my_module_give_this_function_a_nice_name($form, &$form_state) {
// this function gets the $form_state after submitting the form. All values, including the submitted values preserved.
}
Now, we need to print the modal contents.
function my_module_give_this_function_a_nice_name($form, &$form_state) {
// this function gets the $form_state after submitting the form. All values, including the submitted values preserved.
ctools_include('modal');
ctools_modal_add_js();
$commands = array();
$commands[] = ctools_modal_command_display('title', 'modal content goes here');
// You can add extra modal using drupal_add_js/css. You can also have them `#attached` to a rendarable array that you are going to print.
print ajax_render($commands);
exit;
}