2

I am trying to load webform submissions by a particular webform sign_up.

Using Query webform submissions, this does get me all the IDs associated and I can load the data using

$query = \Drupal::entityQuery('webform_submission')
  ->condition('webform_id', 'sign_up');
$result = $query->execute(); // Gives me IDs associated with the "sign_up" webform.

$storage = \Drupal::entityTypeManager()->getStorage('webform_submission');
$submissions = $storage->loadMultiple($result);
$submission_data = array();
foreach ($submissions as $submission) {
  $submission_data[] = $submission->getData();
}
// $submission_data now contains all the submissions from "sign_up" webform.

Is this the correct way of getting the submissions? I would think Webform comes with some sort of API to query the submission results.

I also tried: https://www.drupal.org/project/webform/issues/2902682 but ended up with an empty array.

4

The Webform module stores submission data using a custom Entity-Attribute-Value model. Your example code is the easiest and fastest way to get all the submission data. The only limitation would be the number of submissions vs PHP's memory limit. You will run out of memory if you are loading 1000's submissions

  • If I need to alter and save the data in that foreach loop, do I keep calling $submission->save() or is there like a saveMultiple I can call after the loop? – Ronnie Jan 19 '18 at 22:07
  • Content and config entities don't support save multiple. If you are running out of memory you might need to use Drupal's Batch API. If you are comfortable with SQL you could manually transform and bulk insert the data using custom code. – jrockowitz Jan 21 '18 at 2:38
  • Nope, I wasn't running out of memory just thinking ahead. I was going to do batches if I do end up running out of memory. Thanks again! – Ronnie Jan 21 '18 at 4:30
-1

My example shows one less line to loading the WebformSubmission entity, also uses $webform->hasSubmissions() to check if there are submissions before forging ahead:

// Set the webform.
$webform = Webform::load('sign_up');
if ($webform->hasSubmissions()) {
  $query = \Drupal::entityQuery('webform_submission')
    ->condition('webform_id', 'sign_up');
  $result = $query->execute();
  $submission_data = [];
  foreach ($result as $item) {
    $submission = \Drupal\webform\Entity\WebformSubmission::load($item);
    // Do something with the WebformSubmission item
    $submission_data[] = $submission->getData();
  }
}

so the submission loading line:

$submission = \Drupal\webform\Entity\WebformSubmission::load($item);

replaces:

$storage = \Drupal::entityTypeManager()->getStorage('webform_submission');
$submissions = $storage->loadMultiple($result);

But which is actually better/more efficient, not sure.

  • I'm sorry, but I think this answer contains too much information then actually needed for the actual answer of the original question, making it ambiguous. Therefor downvoting this answer. – Ambidex Jul 23 at 14:25
  • @Ambidex thanks for giving a reason for downvoting, I am not going to remove my answer however as so often the code examples here on SE are not working code. Yes I know there is hook_update specific stuff there, but perhaps you are not a programmer, the question was a coding question? – Duncanmoo Jul 24 at 6:19
  • I'm a coder, though your example includes too much complexity for what's originally being asked. Especially the batch looping adds quite a heavy level of complexity. Novices will probably have quite the challenge to extract the code that is only needed to fetch webform submissions from your example. – Ambidex Jul 24 at 8:38

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.