Drupal is a registered trademark of Dries Buytaert
drupal 11.3.7 Update released for Drupal core (11.3.7)! drupal 11.2.11 Update released for Drupal core (11.2.11)! drupal 10.6.7 Update released for Drupal core (10.6.7)! drupal 10.5.9 Update released for Drupal core (10.5.9)! cms 2.1.1 Update released for Drupal core (2.1.1)! drupal 11.3.6 Update released for Drupal core (11.3.6)! drupal 10.6.6 Update released for Drupal core (10.6.6)! cms 2.1.0 Update released for Drupal core (2.1.0)! bootstrap 8.x-3.40 Minor update available for theme bootstrap (8.x-3.40). menu_link_attributes 8.x-1.7 Minor update available for module menu_link_attributes (8.x-1.7). eca 3.1.1 Minor update available for module eca (3.1.1). layout_paragraphs 2.1.3 Minor update available for module layout_paragraphs (2.1.3). ai 1.3.3 Minor update available for module ai (1.3.3). ai 1.2.14 Minor update available for module ai (1.2.14). node_revision_delete 2.0.3 Minor update available for module node_revision_delete (2.0.3). moderated_content_bulk_publish 2.0.52 Minor update available for module moderated_content_bulk_publish (2.0.52). klaro 3.0.10 Minor update available for module klaro (3.0.10). klaro 3.0.9 Minor update available for module klaro (3.0.9). layout_paragraphs 2.1.2 Minor update available for module layout_paragraphs (2.1.2). geofield_map 11.1.8 Minor update available for module geofield_map (11.1.8).

webform_query

558 sites No security coverage
View on drupal.org

Query Webform Submission Data

The Webform module stores all submission data in the one table using the EAV model. This is great for performance but it can be tricky to extract submission data matching certain criteria.

Webform Query extracts submission IDs based on field values. Currently it supports adding conditions and specifying the webform ID. For example, this would return all "event_registration" submissions for event 1 where the age is 18 or over:

$query = \Drupal::service('webform_query');

$query->addCondition('event', 1)
      ->addCondition('age', 18, '>=')
      ->setWebform('event_registration');
$results = $query->execute();

The return type is an array of objects with a single property "sid", the submission ID. The webform submissions can then be loaded in the normal way.

The webform_id is optional. This would return all webform submissions where the value of the "event" field is 1.

$query = \Drupal::service('webform_query');

$query->addCondition('event', 1);
$results = $query->execute();

Sort the results

$query = \Drupal::service('webform_query');

$query->addCondition('event', 1);
$query->orderBy('age', 'DESC');
$results = $query->execute();

"ASC" is used if the second parameter is missing. The third optional parameter is the table.

$query = \Drupal::service('webform_query');

$query->addCondition('event', 1);
$query->orderBy('created', 'DESC', 'webform_submission');
$results = $query->execute();

Query other tables

Querying other tables which include a sid column is now possible. This is useful for querying the webform_submission table to filter by user, date submitted and other base fields.

addCondition() accepts a table name as an optional fourth parameter.

These tables are queried before webform_submission_data to improve performance.

$query = \Drupal::service('webform_query');

$query->addCondition('event', 1)    
      ->setWebform('event_registration')
      ->addCondition('uid', 1, '=', 'webform_submission');
$results = $query->execute();

Array Queries (IN)

$query = \Drupal::service('webform_query');
$query->addCondition('event', 1)    
      ->setWebform('event_registration')
      ->addCondition('uid', [1, 5, 7], 'IN', 'webform_submission');
$results = $query->processQuery()->fetchCol();
$query = \Drupal::service('webform_query');
$query->addCondition('event', 1)    
      ->setWebform('event_registration')
      ->addCondition('age', [18, 19, 20], 'IN', 'webform_submission_data');
$results = $query->processQuery()->fetchCol();

Additional result types

The method processQuery() returns Drupal\Core\Database\Statement which allows for different result types.

$query = \Drupal::service('webform_query');

$query->addCondition('event', 1)    
      ->setWebform('event_registration')
      ->addCondition('uid', 1, '=', 'webform_submission');
$results = $query->processQuery()->fetchCol();

Activity

Total releases
1
First release
Jan 2025
Latest release
1 year ago
Release cadence
Stability
0% stable

Releases

Version Type Release date
8.x-1.0-alpha4 Pre-release Jan 31, 2025