Entity Query API fills the gap between Drupal 8's built-in RESTful Services and Views with an easy to use, out-of-the-box means for loading, filtering and paging entities in a RESTful style. Essentially, this module provides an API to the Entity QueryInterface.
Any enabled entity gets an endpoint at /entity/{entity_type} which responds to GET requests.
You can apply conditions, sorts, and ranges to your query for the given entity type.
Installation
- Download the module and enable it as usual. There are no dependencies outside Drupal core.
- Navigate to Configuration > Web Services > Entity Query API (/admin/config/services/entityqueryapi).
- Enable the content or configuration entities that you would like to be able to query.
Usage
The following describes Entity Query API's query string syntax. However, if you are doing anything more involved, we recommend using a JavaScript library to craft these query strings for you. The drupal-api package on NPM is maintained in tandem with this module. Even better, Waterwheel is client-side library for taking advantage of Drupal Core's REST module and the Entity Query API, it uses drupal-api under the hood.
Ranges
You can limit your query by adding range parameters to your query string. For example:
?range[start]=0
&range[length]=10
* New lines and tabs just added for readability
Will limit your query to first 10 nodes fetched.
Sorts
You can sort your query by adding sort parameters to your query string. You can have multiple sorts by numbering them. For example:
?sort_0[field]=created
&sort_0[direction]=DESC
&sort_1[field]=title
&sort_1[direction]=ASC
* New lines and tabs just added for readability
Will sort your results by newest nodes, then alphabetically by title.
Conditions
You can filter your query by adding condition parameters to your query string. Like sorts, you can have multiple conditions by numbering them. For example:
?condition_0[field]=created
&condition_0[value]=1459973680
&condition_0[operator]=GT
* New lines and tabs just added for readability
Will filter your results to be all nodes created since the timestamp 1459973680. All operators supported by the QueryInterface::condition method except for 'IN', 'NOT IN', and 'BETWEEN' are supported (future support planned). However, to make url escaping more simple, some of the operators have been remapped to alpha characters. The array below illustrates that mapping.
<?php
$operator_dict = array(
'EQ' => '=',
'NOTEQ' => '<>',
'GT' => '>',
'GTEQ' => '>=',
'LT' => '<',
'LTEQ' => '<=',
);
?>
Condition Groups
You can group conditions using AND and OR groups by adding group parameters. Like sorts and conditions, you can have multiple groups by numbering them. As an example, imagine you want all nodes tagged with 'red' - AND 'blue' OR 'green'. The pseudo-code might look like this: if ( in_array('red', $clrs) && ( in_array('blue', $clrs) || in_array('green', $clrs) ) ). To recreate the OR group between blue and green, you would construct a query string that looks something like this:
?condition_0[field]=color
&condition_0[value]=red
&condition_0[operator]=CONTAINS
&group_0[conjunction]=OR
&condition_1[field]=color
&condition_1[value]=blue
&condition_1[operator]=CONTAINS
&condition_1[group]=group_0
&condition_2[field]=color
&condition_2[value]=green
&condition_2[operator]=CONTAINS
&condition_2[group]=group_0
* New lines and tabs just added for readability
Groups can also be nested within groups. If we were adding a group within group_0 in our previous example, it might look like this: &group_1[conjunction]=AND&group_1[group]=group_0.
Exists and notExists
You can use the exists and notExists shorthands (they're just 'IS NOT NULL' conditions under the hood) too. The query parameter is exists_N, notExists are created by setting the condition subproperty to 'FALSE'. Thus, a notExists look like this:
?exists_0[field]=color
&exists_0[condition]=FALSE
* New lines and tabs just added for readability
Putting It All Together
All of the above parameters can be mixed and matched to describe exactly the query that you want. For example, if we wanted the 5 most recent 'article' nodes created, we would construct a query string as follows:
?condition_0[field]=type
&condition_0[value]=article
&condition_0[operator]=EQ
&sort_0[field]=created
&sort_0[direction]=DESC
&range[start]=0
&range[length]=5
* New lines and tabs just added for readability
Project Information
- Maintenance status: Actively maintained
- Development status: Under active development
- Module categories: Content
- Reported installs: 13 sites currently report using this module. View usage statistics.
- Downloads: 712
- Last modified: June 22, 2016
Stable releases receive coverage from the Drupal Security Team.
Look for the shield icon below.
Downloads
Recommended releases
| Version | Download | Date |
|---|---|---|
| 8.x-1.0-alpha8 | tar.gz (14.92 KB) | zip (22.33 KB) | 2016-Jun-03 |