5

How can I load a webform submission programmatically ?

I've tried : $webform_submission = WebformSubmission::load($sid);

but it return null.

I do this in a custom module call by an handler after webform submission.

7
  • What is the content of the variable $sid?
    – 4uk4
    Jun 7, 2018 at 11:25
  • It's a submission ID
    – Leeroy
    Jun 7, 2018 at 11:32
  • I meant the actual content, this would be the first step to debug this.
    – 4uk4
    Jun 7, 2018 at 11:34
  • I'm getting the submission ID from a custom handler, the data is send by GET to my controller (ID is 1051 currently)
    – Leeroy
    Jun 7, 2018 at 12:44
  • Did you check if the sid 1051 exists in admin/structure/webform/submissions/manage?
    – 4uk4
    Jun 7, 2018 at 13:05

2 Answers 2

11

I think you have to load sid like below

// Load submission using sid.
/** @var \Drupal\webform\WebformSubmissionInterface $webform_submission */
$webform_submission = \Drupal\webform\Entity\WebformSubmission::load($sid);
4

To load webform submissions of a particular webform programmatically -

    $webform = \Drupal\webform\Entity\Webform::load('WEBFORM_ID');  //webform id is the webform name
    if ($webform->hasSubmissions()) {
      $query = \Drupal::entityQuery('webform_submission')
        ->condition('webform_id', 'WEBFORM_ID');
      $result = $query->execute();
      $submission_data = [];
      foreach ($result as $item) {
        $submission = \Drupal\webform\Entity\WebformSubmission::load($item);
        $submission_data = $submission->getData();
      }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.