tokendrupal-8submissiondrupal-webform

How to get submitted form data from passed token


In Drupal 8 (latest version) I have multi step webform created with webform module. 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.

That load() method is expecting $sid (session id) and I'm not sure is this token that id?


Solution

  • Found a solution by looking at webform module confirmation action. 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);
        }