9

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.

2
  • 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
    Commented Aug 25, 2017 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
    Commented Aug 25, 2017 at 9:09

4 Answers 4

8

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();
}
4
  • is there a way to update the submission?
    – batMask
    Commented Sep 24, 2017 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
    Commented Mar 28, 2018 at 10:06
  • How to get the date/time of the submission ? Thx
    – t.c
    Commented Jul 11, 2018 at 16:17
  • I have to say, it's really tacky accepting your own answer when @4uk4 was the one who gave it to you.
    – neuquen
    Commented Jan 20, 2023 at 15:29
4

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)
1
  • If \Drupal::entityQuery('webform_submission')->execute(); gives you an empty array, you can try \Drupal::entityQuery('webform_submission')->accessCheck(FALSE)->execute();
    – alberto56
    Commented Feb 25, 2021 at 20:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.