如何自动加载上次提交(单次提交)?

How to auto load last submission (single submission)?

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

我该怎么做?

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

由于您可能不希望为您的网络表单(或不是所有网络表单)添加额外的网络表单组件,因此您始终可以创建一个模块,包括网络表单功能并使用 [=13= 在网络表单实例上检索提交的数据] 作为:

/**
 * 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
            }

        }

    }

}

希望对您有所帮助。

可以通过 Webform -> Form Settings -> Total Submissions Limit 和 Per User submission limit 来限制每个 webform 只能提交 1 次,如此截图所示

要自动加载用户提交,因为上面不允许您通过使用他们已经提交的网络表单的提交 ID 来显示网络表单。基于此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;