I have a requirement where i need to show webform in ctool modal. When user submit the webform it should show the confirmation message in the same ctool modal. This functionality is being used in my project at several places just the webform to show in ctool modal is changing. I googled it and found this http://drupal.org/node/1196150. The code i found here helped me now i am able to display the webform in ctool modal and form working very well but the confirmation message is not showing in ctool modal. After submission it show the same form in modal rather then confirmation message. This code attache the custom submit handler with form in hook_form_alter and in this submit handler it set the sessoin variable. On this session variable it make decision whether to show form or confirmation message.
here is code
function webform_modal_menu() {
$items['service-page'] = array(
'title' => 'Service page',
'access callback' => TRUE,
'page callback' => 'webform_modal_page',
);
$items['modal/%ctools_js/service-webform'] = array(
'title' => t("service webform"),
'page callback' => 'webform_modal_content',
'file' => 'webform_modal.inc',
'page arguments' => array(1),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
function webform_modal_page() {
ctools_include('ajax'); // Module include the dependence it needs for ajax.
ctools_include('modal');
ctools_modal_add_js();
$output = ctools_modal_text_button(t('Click Here'), 'modal/nojs/service-webform', t('Pop me up'));
$output .= '<div id="modal-message"> </div>';
ctools_include('plugins');
return $output;
}
function webform_modal_content($js = NULL) {
define('WEBFORM_NID', 14);
$webform_nid = WEBFORM_NID; // Your webform $nid
return ctools_ajax_modal_webform($js, $webform_nid);
}
function webform_modal_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'webform_client_form_14') {
$form['#submit'][2] = '_custom_webform_submit';
}
function _custom_webform_submit($form, &$form_state) {
$webform_nid = $form['#node']->nid;
lists_session('webform_client_form_'.$webform_nid, $value = 'submitted');
}
function ctools_ajax_modal_webform($js = NULL, $webform_nid = NULL) {
$node = node_load($webform_nid); // Load webform node
$submission = (object) array(); // empty object required by webform
// React without the modal
if (!$js) {
// Webform requires more parameters than standard forms
return drupal_get_form('webform_client_form_'.$webform_nid, $node, $submission);
}
// React with the modal
// Add modal components
ctools_include('modal');
ctools_include('ajax');
$form_state = array(
'title' => $node->title,
'ajax' => TRUE,
);
// Emulate ctools_modal_form_wrapper() form modal.inc because webform can not be triggered trough drupal_build_form
// If it can, I'd be glad to understand how
$form_state += array(
're_render' => TRUE,
'no_redirect' => !empty($form_state['ajax']),
);
//drupal_get_form('webform_client_form_'.$webform_nid, $node, $submission);
$args = array($node, $submission);
$form_state['build_info']['args'] = $args;
$output = ctools_modal_form_wrapper('webform_client_form_'.$webform_nid, $form_state);
if ( isset($_SESSION['lists']['webform_client_form_'.$webform_nid]) && $_SESSION['lists']['webform_client_form_'.$webform_nid] == 'submitted') {
unset($_SESSION['lists']['webform_client_form_'.$webform_nid]);
$confirm_message = array("#markup" => '<div> Your request has been submitted</div>');
$confirmation['#markup'] =
'<div class="popups-confirmation-wrapper">'.
drupal_render($confirm_message).
'</div>';
$output = array(); // Recreate output
// Oerwrite the form output if it was successful.
$output[] = ctools_modal_command_display('Confirmation', $confirmation);
}
// Render output in modal window
print ajax_render($output);
exit;
}
function lists_session($key, $value = NULL) {
static $storage;
if ($value) {
$storage[$key] = $value ;
$_SESSION['lists'][$key] = $value ; // I put 'lists' in case some other module uses 'type' in $_SESSION
}
else if (empty($storage[$key]) && isset($_SESSION['lists'][$key])) {
$storage[$key] = $_SESSION['lists'][$key];
}
return $storage[$key];
}
When cache is cleared i directly access modal/nojs/service-webform menu item it show the form and hook_form_alter is called. i confirmed this printing some message in form_alter after directly accessing modal/nojs/service-webform now if i access this url using 'Click here' button which i created on serice-page then it also show that form alter is called and print the message on console(because i am using chromephp for logging.). But if cache is cleared and i click on 'Click here' then form is shown in modal but form_alter is not called because it doesn't print any message in console. After clicking on that button if i access modal/nojs/service-webform directly then also form_alter is not being called. So problem is that custom submit handler is not being called. i confirmed this printing $form variable in form alter. This may make more clear http://drupal.org/node/1196150#comment-6458176 Is there any cache issue or i am making some mistake?