6

I'm attempting to perform a query on webform submissions so I can display all submissions submitted by a specific user. However I'm not sure how to make this query. I made a guess that webform submissions were nodes themselves and performed a query like this

    $query = \Drupal::entityQuery('node')
        ->condition('type', 'webform');
    $nids = $query->execute();
    die(print_r($nids));

But it produced no results. Is there a way I can perform a query on webform submissions so that I can select all submissions authored by a specific user?

UPDATE: Looking through the webform module, I did see some code querying submissions.

$query = $this->getQuery();
$query->condition('webform_id', $webform->id());
$query->condition('in_draft', FALSE);
$query->range(0, 1);

This is exactly the kind of query I want to perform but when I try to use getQuery() I get

Call to undefined function Drupal\my_module\my_helper_class\getQuery()

I understand how to start entity queries for nodes like this

 \Drupal::entityQuery('node')

But is there a way to start an entity query without specifying the entity type? Or is there a way to find out what kind of entity type webform queries are?

  • You get that error because getQuery() is only a method of the class that $this is referring to. Could you build a simple view to do this and just feed it a UID argument? – Kevin Mar 6 '17 at 18:47
  • Unfortunately no, I cannot use a view in this situation since there is a lot more that needs to occur within this module other than just displaying the results. Displaying the submissions is just a small part of the plugin. – Matt Mar 6 '17 at 18:52
8

Webform submissions are content entities, so something like this should work:

$query = \Drupal::entityQuery('webform_submission')
    ->condition('uid', $uid);
$result = $query->execute();
  • This worked! But how did you figure out that webform_submission was the proper entity name? – Matt Mar 6 '17 at 19:01
  • 2
    It's easy when you know some of the conventions - all entity types live in Drupal\MODULENAME\Entity, so I just downloaded the module, looked in src/Entity, found WebformSubmission, and checked its ID in the entity type annotation – Clive Mar 6 '17 at 19:04
  • 1
    Thanks that helps a ton! I'm sure that will help solve a lot of headaches in the future. – Matt Mar 6 '17 at 19:07
  • Would it be possible to user fields aside from id, created, and other base field definitions in a query? For instance if I added 'quantity' as a form field, is there a way I can do ->condition('quantity',5)? or ->sort('quantity','desc') I attempted using that syntax but it did not work. – Matt Aug 28 '17 at 20:33
  • Is there way to update an custom field in the webform submission by ~sid~ – batMask Sep 24 '17 at 14:04

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.