简体   繁体   English

如何从传递的令牌中获取提交的表单数据

[英]How to get submitted form data from passed token

In Drupal 8 (latest version) I have multi step webform created with webform module. 在Drupal 8(最新版本)中,我使用webform模块创建了多步webform。 After last step submitted I end up on confirmation page, which url looks like: 提交完最后一步后,我最终确认页面,该网址如下:

/node/1/webform/confirmation?token=KxCIo9eUxHC_XJKtDG8erszn5BL5UHBZnRrvJU7Kirw

Now, I want to create custom confirmation page and I already created module which creates custom page and that works. 现在,我想创建自定义确认页面,我已经创建了创建自定义页面的模块。 It suppose to be called the similar way: 它假设被称为类似的方式:

/confirmation?token=KxCIo9eUxHC_XJKtDG8erszn5BL5UHBZnRrvJU7Kirw

My question is: how can I use this token value to collect submitted form data? 我的问题是:如何使用此令牌值来收集提交的表单数据?

Tried something like this from my confirmation page controller: 从我的确认页面控制器尝试这样的事情:

        $token = \Drupal::request()->query->get('token');

        $webform_submission = \Drupal\webform\Entity\WebformSubmission::load($token);

        // Get submission data.
        $data = $webform_submission->getData();

//        var_dump($data);

$token value is collected well, but when I try to use it to get that $webform_submissions I get null value and collecting $data of course fails at the next row. 收集$token值很好,但是当我尝试使用它来获取$webform_submissions我得到null值并且收集$data当然会在下一行失败。

That load() method is expecting $sid (session id) and I'm not sure is this token that id? 那个load()方法期待$sid (会话ID),我不确定这个标记是id吗?

Found a solution by looking at webform module confirmation action. 通过查看webform模块确认操作找到解决方案。 Goes like this: 像这样:

    $token = \Drupal::request()->query->get('token');


    if ($token) {
        /** @var \Drupal\webform\WebformSubmissionStorageInterface $webform_submission_storage */
        $webform_submission_storage = \Drupal::entityTypeManager()->getStorage('webform_submission');
        if ($entities = $webform_submission_storage->loadByProperties(['token' => $token])) {
            $webform_submission = reset($entities);
        }
        $data = $webform_submission->getData();

        var_dump($data);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM