没有调用drupal 7 ctool模态表单自定义提交处理程序

时间:2012-09-11 15:28:24

标签: drupal drupal-webform drupal-ctools

我有一个要求,我需要在ctool模式中显示webform。当用户提交webform时,它应该在同一个ctool模式中显示确认消息。这个功能正在我的项目中的几个地方使用,只是在ctool模式中显示的webform正在改变。我用Google搜索并找到了http://drupal.org/node/1196150。我在这里找到的代码帮助我现在我能够以ctool模态显示webform并且表单工作得很好但是确认消息没有显示在ctool模态中。提交后,它在模态而不是确认消息中显示相同的表单。此代码使用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">&nbsp</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之后确认这打印了一些消息,如果我使用我在serice-page上创建的'Click here'按钮访问这个url然后它还显示调用了form alter并打印消息在控制台上(因为我使用chromephp进行日志记录。)。但是如果清除缓存并且我单击“单击此处”,则表单以模态显示,但不调用form_alter,因为它不会在控制台中打印任何消息。点击该按钮后,如果我直接访问modal / nojs / service-webform,那么也不会调用form_alter。所以问题是没有调用自定义提交处理程序。我在形式alter中确认了这个打印$ form变量。这可能会更清楚http://drupal.org/node/1196150#comment-6458176是否有任何缓存问题或我犯了一些错误?

0 个答案:

没有答案