6

In Drupal 8 there is no function webform_get_submissions() as of this writing.

Webforms can be attached to nodes. How to get all the submissions of a webform that belong to a specific node (e. g. node ID = 15):

webform_get_submissions_by_node_id() ... is the wanted function.

There are similar questions here but the PHP code to get only the submissions which where created on one specific node is different. And for this there is still now answer to find.

  • I do have the feeling: if you can attach a webform to a node, and you can get the webform based on the submissions, then you can also get the node by submissions. Just take a look in PhpMyAdmin or anywhere else, which tables stores the information. Am also sure you can do it via Drupal API. Do you need help for this? – ssibal Aug 25 '17 at 8:45
  • " ... then you can also get the node by submissions": I do not want to get the node by submissions, I want to get the submissions by node. Function webform_get_submissions_by_node_id() ... this is the wanted function. – drupalfan Aug 25 '17 at 9:09
7

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();
}
  • is there a way to update the submission? – nanbatman Sep 24 '17 at 15:34
  • This is great. Is there an easy way to format the submission data into a pretty table, similar to the submissions table that comes with webform? I'm struggling on this next step … – Hendrik Mar 28 '18 at 10:06
  • How to get the date/time of the submission ? Thx – t.c Jul 11 '18 at 16:17
3

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',
]);
3

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);
1

You can also make use of Drupal's entityQuery() for making advanced filters/conditions:

$query = \Drupal::entityQuery('webform_submission')
                ->condition('entity_type', 'node')
                ->condition('entity_id', 123, '='); // optional operator
$results = $query->execute();

Note that $results will only contains SID (submission id), you may need to load them via its entity loader:

\Drupal\webform\Entity\WebformSubmission::loadMultiple($results)

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.