Hi! How can I fetch form submissions that belong to a webform that is referenced in a content type? I have created the webform and have also created a reference field for that form, in one of my content types. This works great because I'm able to use a "global" form but it encapsulates the submissions on each node of the content type. I am able to see the specific submissions that are attached by navigation to the "results" tab in the admin section of a node.

My issue is that I can't seem to figure out how to programmatically retrieve these results that are associated with the specific node. Is this something easy? I've been searching around and can't seem to find a solution. Thanks in advance!

Comments

Rulito created an issue. See original summary.

jrockowitz’s picture

If you are using an EntityQuery, you can call WebformSubmissionStorage::addQueryConditions helper method with your node as the $source_entity.

The code would look something like...

      $submission_storage = \Drupal::entityTypeManager()->getStorage('webform_submission');
      $entity_query = $submission_storage->getQuery();
      $webform = Webform::load('WEBFORM_ID');
      $node = Node::load('NODE_ID');
      $submission_storage->addQueryCondition($entity_query, $webform, $node);
      // Add entity query conditions as needed.
Rulito’s picture

Thanks for the quick response! I added my corresponding ID's to the code you gave me and now getting the following error:

Call to undefined method Drupal\webform\WebformSubmissionStorage::addQueryCondition()

Any insight? Here's my current logic:

use Drupal\webform\Entity\Webform;
use Drupal\node\Entity\Node;

function party_form_form_webform_submission_party_registration_form_alter(&$form, $form_state, $form_id){

  $submission_storage = \Drupal::entityTypeManager()->getStorage('webform_submission');
  $entity_query = $submission_storage->getQuery();
  $webform = Webform::load('webform_submission_party_registration_form');
  $node = Node::load('1');
  $submission_storage->addQueryCondition($entity_query, $webform, $node);

}

Thanks again for the help and for such an awesome module, really appreciate it!

jrockowitz’s picture

There was a typo on my part. Please use $submission_storage->addQueryConditions w/ the 's'

jrockowitz’s picture

Priority: Major » Minor
Rulito’s picture

Status: Active » Fixed

Perfect! Exactly what I needed. Thanks so much for the help!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.