【问题标题】:How to auto load last submission (single submission)?如何自动加载最后一次提交(单次提交)?
【发布时间】:2015-06-04 20:15:49
【问题描述】:

我在 Drupal 中构建了多个网络表单。我希望用户

  • 按表格只提交一份。
  • 当他们再次提交网络表单时自动加载上次提交。

我该怎么做?

【问题讨论】:

    标签: forms drupal submission drupal-webform


    【解决方案1】:
    • 您可以使用电子邮件网络表单组件,将其设置为unique 并检查User email as default,如果您的网络表单中有一个。
    • 通过创建一个带有上下文过滤器的视图块给用户提供提供的 uid 和与 webform 的关系,可以自动加载 webform 提交。

    由于您可能不希望为您的网络表单(或不是全部)添加额外的网络表单组件,您始终可以创建一个模块,包含网络表单功能并使用hook_form_alter() 检索在网络表单实例上提交的数据:

    /**
     * Implements hook_form_alter().
     */
    function YOUR_MODULE_form_alter(&$form, &$form_state, $form_id) {
    
        // get current user
        global $user;
    
        // include webform functionality
        module_load_include('inc','webform','includes/webform.submissions');
    
        // make sure to only alter webform forms
        if (strpos($form_id, 'webform_client_form_') !== FALSE) {
    
            // check if the user is a an authenticated user
            if (in_array('authenticated user', $user->roles)) {
    
                // build $filters array for submission retrieval
                $filters = array(
                    'nid' => $form['#node']->webform['nid'],
                    'uid' => $user->uid,
                );
    
                /** 
                 * When not using a unique webform component for 1 submission
                 * you can use a submission of the user on a webform instance
                 * to prevent another submission.
                 */
    
                // get submissions of the user by webform nid
                if ($submissions = webform_get_submissions($filters)) {
    
                    // disable the form to limit multiple submissions per instance
                    $form['#disabled'] = TRUE;
    
                    /**
                     * Webform instance submission data of the current user
                     * can be found in $submissions[1]->data array
                     */
    
                    # render your submission with Form api
                }
    
            }
    
        }
    
    }
    

    希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      可以通过 Webform -> Form Settings -> Total Submissions Limit 和 Per User submit limit 来限制每个网络表单只能提交 1 次,如此屏幕截图所示

      要自动加载用户提交,因为上面不允许您通过使用他们已经提交的 webform 的提交 id 来显示 webform。基于此code

      module_load_include('inc','webform','includes/webform.submissions');
      $sid = 10;
      $submission = webform_get_submissions(array('sid' => $sid));
      $nid = $submission[$sid]->nid;
      
      $web_submission = webform_get_submission($nid, $sid);
      $node = node_load($nid);
      $output = webform_submission_render($node, $web_submission, NULL, 'html');
      
      print $output;
      

      【讨论】:

        猜你喜欢
        • 2011-02-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-24
        • 1970-01-01
        • 2012-01-01
        • 1970-01-01
        • 2011-01-21
        相关资源
        最近更新 更多