3 namespace Drupal\Core\Entity\Query;
5 use Drupal\Core\Database\Query\PagerSelectExtender;
6 use Drupal\Core\Entity\EntityTypeInterface;
9 * The base entity query class.
11 abstract class QueryBase implements QueryInterface {
14 * The entity type this query runs against.
18 protected $entityTypeId;
21 * Information about the entity type.
23 * @var \Drupal\Core\Entity\EntityTypeInterface
25 protected $entityType;
35 * TRUE if this is a count query, FALSE if it isn't.
39 protected $count = FALSE;
44 * @var \Drupal\Core\Entity\Query\ConditionInterface
49 * The list of aggregate expressions.
53 protected $aggregate = [];
56 * The list of columns to group on.
60 protected $groupBy = [];
63 * Aggregate Conditions
65 * @var \Drupal\Core\Entity\Query\ConditionAggregateInterface
67 protected $conditionAggregate;
70 * The list of sorts over the aggregate results.
74 protected $sortAggregate = [];
81 protected $range = [];
84 * The query metadata for alter purposes.
88 protected $alterMetaData;
98 * Whether access check is requested or not. Defaults to TRUE.
102 protected $accessCheck = TRUE;
105 * Flag indicating whether to query the current revision or all revisions.
109 protected $allRevisions = FALSE;
112 * The query pager data.
116 * @see Query::pager()
118 protected $pager = [];
121 * List of potential namespaces of the classes belonging to this query.
125 protected $namespaces = [];
128 * Constructs this object.
130 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
131 * The entity type definition.
132 * @param string $conjunction
133 * - AND: all of the conditions on the query need to match.
134 * - OR: at least one of the conditions on the query need to match.
135 * @param array $namespaces
136 * List of potential namespaces of the classes belonging to this query.
138 public function __construct(EntityTypeInterface $entity_type, $conjunction, array $namespaces) {
139 $this->entityTypeId = $entity_type->id();
140 $this->entityType = $entity_type;
141 $this->conjunction = $conjunction;
142 $this->namespaces = $namespaces;
143 $this->condition = $this->conditionGroupFactory($conjunction);
144 if ($this instanceof QueryAggregateInterface) {
145 $this->conditionAggregate = $this->conditionAggregateGroupFactory($conjunction);
152 public function getEntityTypeId() {
153 return $this->entityTypeId;
159 public function condition($property, $value = NULL, $operator = NULL, $langcode = NULL) {
160 $this->condition->condition($property, $value, $operator, $langcode);
167 public function exists($property, $langcode = NULL) {
168 $this->condition->exists($property, $langcode);
175 public function notExists($property, $langcode = NULL) {
176 $this->condition->notExists($property, $langcode);
183 public function range($start = NULL, $length = NULL) {
192 * Creates an object holding a group of conditions.
194 * See andConditionGroup() and orConditionGroup() for more.
196 * @param string $conjunction
197 * - AND (default): this is the equivalent of andConditionGroup().
198 * - OR: this is the equivalent of orConditionGroup().
200 * @return \Drupal\Core\Entity\Query\ConditionInterface
201 * An object holding a group of conditions.
203 protected function conditionGroupFactory($conjunction = 'AND') {
204 $class = static::getClass($this->namespaces, 'Condition');
205 return new $class($conjunction, $this, $this->namespaces);
211 public function andConditionGroup() {
212 return $this->conditionGroupFactory('and');
218 public function orConditionGroup() {
219 return $this->conditionGroupFactory('or');
225 public function sort($field, $direction = 'ASC', $langcode = NULL) {
228 'direction' => strtoupper($direction),
229 'langcode' => $langcode,
237 public function count() {
245 public function accessCheck($access_check = TRUE) {
246 $this->accessCheck = $access_check;
253 public function currentRevision() {
254 $this->allRevisions = FALSE;
261 public function allRevisions() {
262 $this->allRevisions = TRUE;
269 public function pager($limit = 10, $element = NULL) {
270 // Even when not using SQL, storing the element PagerSelectExtender is as
271 // good as anywhere else.
272 if (!isset($element)) {
273 $element = PagerSelectExtender::$maxElement++;
275 elseif ($element >= PagerSelectExtender::$maxElement) {
276 PagerSelectExtender::$maxElement = $element + 1;
281 'element' => $element,
287 * Gets the total number of results and initialize a pager for the query.
289 * The pager can be disabled by either setting the pager limit to 0, or by
290 * setting this query to be a count query.
292 protected function initializePager() {
293 if ($this->pager && !empty($this->pager['limit']) && !$this->count) {
294 $page = pager_find_page($this->pager['element']);
295 $count_query = clone $this;
296 $this->pager['total'] = $count_query->count()->execute();
297 $this->pager['start'] = $page * $this->pager['limit'];
298 pager_default_initialize($this->pager['total'], $this->pager['limit'], $this->pager['element']);
299 $this->range($this->pager['start'], $this->pager['limit']);
306 public function tableSort(&$headers) {
307 // If 'field' is not initialized, the header columns aren't clickable.
308 foreach ($headers as $key => $header) {
309 if (is_array($header) && isset($header['specifier'])) {
310 $headers[$key]['field'] = '';
314 $order = tablesort_get_order($headers);
315 $direction = tablesort_get_sort($headers);
316 foreach ($headers as $header) {
317 if (is_array($header) && ($header['data'] == $order['name'])) {
318 $this->sort($header['specifier'], $direction, isset($header['langcode']) ? $header['langcode'] : NULL);
326 * Makes sure that the Condition object is cloned as well.
328 public function __clone() {
329 $this->condition = clone $this->condition;
335 public function addTag($tag) {
336 $this->alterTags[$tag] = 1;
343 public function hasTag($tag) {
344 return isset($this->alterTags[$tag]);
350 public function hasAllTags() {
351 return !(boolean)array_diff(func_get_args(), array_keys($this->alterTags));
357 public function hasAnyTag() {
358 return (boolean)array_intersect(func_get_args(), array_keys($this->alterTags));
364 public function addMetaData($key, $object) {
365 $this->alterMetaData[$key] = $object;
372 public function getMetaData($key) {
373 return isset($this->alterMetaData[$key]) ? $this->alterMetaData[$key] : NULL;
379 public function aggregate($field, $function, $langcode = NULL, &$alias = NULL) {
380 if (!isset($alias)) {
381 $alias = $this->getAggregationAlias($field, $function);
384 $this->aggregate[$alias] = [
386 'function' => $function,
388 'langcode' => $langcode,
397 public function conditionAggregate($field, $function = NULL, $value = NULL, $operator = '=', $langcode = NULL) {
398 $this->aggregate($field, $function, $langcode);
399 $this->conditionAggregate->condition($field, $function, $value, $operator, $langcode);
407 public function sortAggregate($field, $function, $direction = 'ASC', $langcode = NULL) {
408 $alias = $this->getAggregationAlias($field, $function);
410 $this->sortAggregate[$alias] = [
412 'function' => $function,
413 'direction' => $direction,
414 'langcode' => $langcode,
416 $this->aggregate($field, $function, $langcode, $alias);
424 public function groupBy($field, $langcode = NULL) {
427 'langcode' => $langcode,
434 * Generates an alias for a field and it's aggregated function.
436 * @param string $field
437 * The field name used in the alias.
438 * @param string $function
439 * The aggregation function used in the alias.
442 * The alias for the field.
444 protected function getAggregationAlias($field, $function) {
445 return strtolower($field . '_' . $function);
449 * Gets a list of namespaces of the ancestors of a class.
452 * An object within a namespace.
455 * A list containing the namespace of the class, the namespace of the
456 * parent of the class and so on and so on.
458 public static function getNamespaces($object) {
460 for ($class = get_class($object); $class; $class = get_parent_class($class)) {
461 $namespaces[] = substr($class, 0, strrpos($class, '\\'));
467 * Finds a class in a list of namespaces.
469 * @param array $namespaces
470 * A list of namespaces.
471 * @param string $short_class_name
472 * A class name without namespace.
475 * The fully qualified name of the class.
477 public static function getClass(array $namespaces, $short_class_name) {
478 foreach ($namespaces as $namespace) {
479 $class = $namespace . '\\' . $short_class_name;
480 if (class_exists($class)) {