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?

2
  • 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
    Commented Mar 6, 2017 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.
    – Amy
    Commented Mar 6, 2017 at 18:52

2 Answers 2

11

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

$query = \Drupal::entityQuery('webform_submission')
    ->condition('uid', $uid)
    ->accessCheck(FALSE);
$result = $query->execute();
6
  • This worked! But how did you figure out that webform_submission was the proper entity name?
    – Amy
    Commented Mar 6, 2017 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
    Commented Mar 6, 2017 at 19:04
  • 1
    Thanks that helps a ton! I'm sure that will help solve a lot of headaches in the future.
    – Amy
    Commented Mar 6, 2017 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.
    – Amy
    Commented Aug 28, 2017 at 20:33
  • 2
    Matt, you'd need to query the table directly. See my answer here: drupal.stackexchange.com/a/267306/34673
    – imclean
    Commented Aug 10, 2018 at 6:17
2

Remember to include accessCheck(FALSE) or you won't get any result.

$query = \Drupal::entityQuery('webform_submission')
    ->condition('uid', $uid)
    ->accessCheck(FALSE);
$result = $query->execute(); 

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.