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
0

So not really the OP but I thought my example below would be useful. Scenario: Using hook_update_n delete all webform submissions for a specific webform"

/**
 * Delete all past submissions of the sign_up webform
 */
function sign_up_update_8003(&$sandbox) {
  // Set the webform.
  $webform = Webform::load('sign_up');
  if ($webform->hasSubmissions()) {
    $query = \Drupal::entityQuery('webform_submission')
      ->condition('webform_id', 'sign_up');
    $result = $query->execute();
    if (!isset($sandbox['total'])) {
      $sandbox['total'] = count($result);
    }
    // Use 50 for a batch run
    $result = array_slice($result, 0, 50, TRUE);
    foreach ($result as $item) {
      $submission = \Drupal\webform\Entity\WebformSubmission::load($item);
      $submission->delete();
      $sandbox['current']++;
    }
    $sandbox['#finished'] = ($sandbox['current'] / $sandbox['total']);
  }
  else {
    $sandbox['#finished'] = 1;
  }
}

One could use the above to run through all webform submissions and perform some other action on the submission data, but my code is dependent on deleting the records (otherwise you have an infinite loop there).

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.