6

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
    – Mistraë
    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)
    – Mistraë
    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

12

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 you can use the following snippet. All you need to know is the webform ID (webform machine name):

$webform_id = 'MY_WEBFORM_ID';

$webform = \Drupal\webform\Entity\Webform::load($webform_id);  
if ($webform->hasSubmissions()) {

  $query = \Drupal::entityQuery('webform_submission')
    ->accessCheck(FALSE)
    ->condition('webform_id', $webform_id);
  $result = $query->execute();

  $submission_data = [];
  foreach ($result as $item) {

    $submission = \Drupal\webform\Entity\WebformSubmission::load($item);
    $submission_data[$submission->id()] = $submission->getData();
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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