Drupal - Programmatically get webform submissions of a specific nodes webform
Thank you 4k4. Here is now the correct and full code for use in mytheme.theme file (we need the getData() function:
$storage = \Drupal::entityTypeManager()->getStorage('webform_submission');
$webform_submission = $storage->loadByProperties([
'entity_type' => 'node',
'entity_id' => $variables['node']->id(),
]);
$submission_data = array();
foreach ($webform_submission as $submission) {
$submission_data[] = $submission->getData();
}
In the Drupal 8 version of Webform submissions are stored in an entity and you can retrieve them like any other entitiy, for example by using loadByProperties()
:
$storage = \Drupal::entityTypeManager()->getStorage('webform_submission');
$submissions = $storage->loadByProperties([
'entity_type' => 'node',
'entity_id' => '123',
]);
All the current answers are correct but loading submissions by a webform, source entity, and/or account should a little easier, so I created Issue #2954515: Add WebformSubmissionStorage::loadByEntities method which does require you to have the fully loaded entities to load the related submissions.
$storage = \Drupal::entityTypeManager()->getStorage('webform_submission');
$webform_submissions = $storage->loadByEntities($webform, $node);