我有一个要求,我需要在 ctool 模式中显示网络表单。当用户提交网络表单时,它应该在相同的 ctool 模式中显示确认消息。这个功能正在我的项目中的几个地方使用,只是在 ctool 模式中显示的 web 表单正在发生变化。我用谷歌搜索并找到了这个http://drupal.org/node/1196150。我在这里找到的代码对我有帮助,现在我可以在 ctool modal 中显示 webform,并且表单工作得很好,但是确认消息没有在 ctool modal 中显示。提交后,它以模态显示相同的表单,而不是确认消息。此代码将自定义提交处理程序与 hook_form_alter 中的表单附加在一起,并在此提交处理程序中设置 sessoin 变量。在此会话变量上,它决定是否显示表单或确认消息。
这是代码
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];
}
清除缓存后,我直接访问 modal/nojs/service-webform 菜单项,它显示表单并调用 hook_form_alter。我现在在直接访问 modal/nojs/service-webform 后确认了在 form_alter 中打印了一些消息在控制台上(因为我使用 chromephp 进行日志记录。)。但是,如果缓存被清除并且我单击“单击此处”,则表单以模式显示,但不会调用 form_alter,因为它不会在控制台中打印任何消息。单击该按钮后,如果我直接访问 modal/nojs/service-webform,则不会调用 form_alter。所以问题是没有调用自定义提交处理程序。我在表单更改中确认了这个打印 $form 变量。http://drupal.org/node/1196150#comment-6458176是否有任何缓存问题或者我犯了一些错误?