Score:1

Why does this code not load webform submissions?

cn flag

I’m trying to load all submissions from a specific webform, and for some reason it always returns no results (even though I know it has results from the UI). I found this bit of code that I adapted for my needs:

$webform = \Drupal\webform\Entity\Webform::load($webform_id);

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();
  }
}

However for some reason the results are always an empty array, even if the $webform->hasSubmissions() returns true . Not sure why this code isn’t working? Seems simple enough?

id flag
WebformSubmission::load expects an identifier. What is the data type of $item?
Rene avatar
cn flag
Good question, but it's not even getting there as `$result = $query->execute();` returns an empty array, even though I have forms results.
cn flag
Where is the code executed, or more to the point in the context of what user account? Entity queries are access-sensitive
Rene avatar
cn flag
Code is executed in a custom service that I created
id flag
Is the webform ID correct? It seems like this is becoming an interview...
Rene avatar
cn flag
The form ID is indeed correct, as the `$webform = \Drupal\webform\Entity\Webform::load($webform_id);` line returns the webform object when I debug it. I appreciate the follow up.
cn flag
How is the service consumed? Is it a route? Form submission? Cron job? If the latter you'll need `->accessCheck(FALSE)` on the query. If you're executing in the context of a user session where the account doesn't have permissions to view the entities you're loading, you'll need the same thing
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.