3 namespace Drupal\Core\Entity\Query;
5 use Drupal\Core\Database\Query\AlterableInterface;
8 * Interface for entity queries.
10 * Never instantiate classes implementing this interface directly. Always use
11 * the QueryFactory class.
15 interface QueryInterface extends AlterableInterface {
18 * Gets the ID of the entity type for this query.
22 public function getEntityTypeId();
25 * Add a condition to the query or a condition group.
27 * For example, to find all entities containing both the Turkish 'merhaba'
28 * and the Polish 'siema' within a 'greetings' text field:
30 * $entity_ids = \Drupal::entityQuery($entity_type)
31 * ->condition('greetings', 'merhaba', '=', 'tr')
32 * ->condition('greetings.value', 'siema', '=', 'pl')
37 * Name of the field being queried. It must contain a field name, optionally
38 * followed by a column name. The column can be the reference property,
39 * usually "entity", for reference fields and that can be followed
40 * similarly by a field name and so on. Additionally, the target entity type
41 * can be specified by appending the ":target_entity_type_id" to "entity".
47 * - tags.entity:taxonomy_term.name
49 * - uid.entity:user.name
50 * "tags" "is the same as "tags.value" as value is the default column.
51 * If two or more conditions have the same field names they apply to the
52 * same delta within that field. In order to limit the condition to a
53 * specific item a numeric delta should be added between the field name and
56 * ->condition('tags.5.value', 'news')
58 * This will require condition to be satisfied on a specific delta of the
59 * field. The condition above will require the 6th value of the field to
60 * match the provided value. Further, it's possible to create a condition on
61 * the delta itself by using '%delta'. For example,
63 * ->condition('tags.%delta', 5)
65 * will find only entities which have at least six tags. Finally, the
66 * condition on the delta itself accompanied with a condition on the value
67 * will require the value to appear in the specific delta range. For
70 * ->condition('tags.%delta', 0, '>'))
71 * ->condition('tags.%delta.value', 'news'))
73 * will only find the "news" tag if it is not the first value. It should be
74 * noted that conditions on specific deltas and delta ranges are only
75 * supported when querying content entities.
77 * The value for $field. In most cases, this is a scalar and it's treated as
78 * case-insensitive. For more complex operators, it is an array. The meaning
79 * of each element in the array is dependent on $operator.
82 * - '=', '<>', '>', '>=', '<', '<=', 'STARTS_WITH', 'CONTAINS',
83 * 'ENDS_WITH': These operators expect $value to be a literal of the
84 * same type as the column.
85 * - 'IN', 'NOT IN': These operators expect $value to be an array of
86 * literals of the same type as the column.
87 * - 'BETWEEN': This operator expects $value to be an array of two literals
88 * of the same type as the column.
90 * Language code (optional). If omitted, any translation satisfies the
91 * condition. However, if two or more conditions omit the langcode within
92 * one condition group then they are presumed to apply to the same
93 * translation. If within one condition group one condition has a langcode
94 * and another does not they are not presumed to apply to the same
97 * @return \Drupal\Core\Entity\Query\QueryInterface
98 * @see \Drupal\Core\Entity\Query\andConditionGroup
99 * @see \Drupal\Core\Entity\Query\orConditionGroup
101 public function condition($field, $value = NULL, $operator = NULL, $langcode = NULL);
104 * Queries for a non-empty value on a field.
109 * Language code (optional).
110 * @return \Drupal\Core\Entity\Query\QueryInterface
112 public function exists($field, $langcode = NULL);
115 * Queries for an empty field.
120 * Language code (optional).
121 * @return \Drupal\Core\Entity\Query\QueryInterface
123 public function notExists($field, $langcode = NULL);
126 * Enables a pager for the query.
129 * An integer specifying the number of elements per page. If passed a false
130 * value (FALSE, 0, NULL), the pager is disabled.
132 * An optional integer to distinguish between multiple pagers on one page.
133 * If not provided, one is automatically calculated.
135 * @return \Drupal\Core\Entity\Query\QueryInterface
138 public function pager($limit = 10, $element = NULL);
142 * @param null $length
143 * @return \Drupal\Core\Entity\Query\QueryInterface
146 public function range($start = NULL, $length = NULL);
151 * @param string $direction
153 * Language code (optional).
154 * @return \Drupal\Core\Entity\Query\QueryInterface
157 public function sort($field, $direction = 'ASC', $langcode = NULL);
160 * Makes this a count query.
162 * For count queries, execute() returns the number entities found.
164 * @return \Drupal\Core\Entity\Query\QueryInterface
167 public function count();
170 * Enables sortable tables for this query.
173 * An array of headers of the same structure as described in
174 * template_preprocess_table(). Use a 'specifier' in place of a 'field' to
175 * specify what to sort on. This can be an entity or a field as described
178 * @return \Drupal\Core\Entity\Query\QueryInterface
181 public function tableSort(&$headers);
184 * @return \Drupal\Core\Entity\Query\QueryInterface
187 public function accessCheck($access_check = TRUE);
193 * Returns an integer for count queries or an array of ids. The values of
194 * the array are always entity ids. The keys will be revision ids if the
195 * entity supports revision and entity ids if not.
197 public function execute();
200 * Creates a new group of conditions ANDed together.
202 * For example, consider a drawing entity type with a 'figures' multi-value
203 * field containing 'shape' and 'color' columns. To find all drawings
204 * containing both a red triangle and a blue circle:
206 * $query = \Drupal::entityQuery('drawing');
207 * $group = $query->andConditionGroup()
208 * ->condition('figures.color', 'red')
209 * ->condition('figures.shape', 'triangle');
210 * $query->condition($group);
211 * $group = $query->andConditionGroup()
212 * ->condition('figures.color', 'blue')
213 * ->condition('figures.shape', 'circle');
214 * $query->condition($group);
215 * $entity_ids = $query->execute();
218 * @return \Drupal\Core\Entity\Query\ConditionInterface
220 public function andConditionGroup();
223 * Creates a new group of conditions ORed together.
225 * For example, consider a map entity with an 'attributes' field
226 * containing 'building_type' and 'color' columns. To find all green and
229 * $query = \Drupal::entityQuery('map');
230 * $group = $query->orConditionGroup()
231 * ->condition('attributes.color', 'red')
232 * ->condition('attributes.color', 'green');
233 * $entity_ids = $query
234 * ->condition('attributes.building_type', 'bikeshed')
235 * ->condition($group)
238 * Note that this particular example can be simplified:
240 * $entity_ids = $query
241 * ->condition('attributes.color', array('red', 'green'))
242 * ->condition('attributes.building_type', 'bikeshed')
246 * @return \Drupal\Core\Entity\Query\ConditionInterface
248 public function orConditionGroup();
251 * Queries the current revision.
255 public function currentRevision();
258 * Queries the latest revision.
260 * The latest revision is the most recent revision of an entity. This will be
261 * either the default revision, or a pending revision if one exists and it is
262 * newer than the default.
266 public function latestRevision();
269 * Queries all the revisions.
273 public function allRevisions();