nortmas
9/27/2017 - 4:05 PM

Entity query

<?php

use Drupal\Core\Config\Entity\Query\Query;
use Drupal\Core\Entity\Query\QueryFactory;
use Drupal\Core\Entity\Query\QueryInterface;

// Condition operators (3rd param):
 '=', '<>', '>', '>=', '<', '<=', 'STARTS_WITH', 'CONTAINS', 'ENDS_WITH', 'IN', 'NOT IN', 'BETWEEN'.

////////////////////////
////////////////////////
//      IMPORTANT     //
/////////////////////////
/////////////////////////
// To make entityQuery work correctly bypassing the node pemission access restriction
// YOU must add the folowing condintion:
->addMetaData('account', User::load(1))


$this->entityQuery = \Drupal::entityTypeManager()->getStorage('node')->getQuery();

// Returns an aggregated query object for a given entity type.
$query = $this->entityQuery->getAggregate('node', 'AND');

// For example I want to filter nodes whose NIDs are between 10 and 50. 
// But I would also like to see which NID is the lowest and which is the highest.
$result = \Drupal::entityQueryAggregate('node')
  ->aggregate('nid', 'MIN', NULL, 'nid_min')
  ->aggregate('nid', 'MAX', NULL, 'nid_max')
  ->condition('nid', [10, 50], 'BETWEEN')
  ->execute();
// $result[0]['nid_min'] and $result[0]['nid_max']

// groupBY() alone doesn't do anything. Only in combination with aggregate.
$query = Drupal::entityQueryAggregate('node');
$result = $query
  ->groupBy('type')
  ->aggregate('nid', 'COUNT')
  ->execute();
  
// Conditions with related user entity example.
$node_ids = $this->entityQuery->get('node')
  ->condition('type', 'article')
  ->condition('status', 1)
  ->condition('uid.entity.name', '%admin%', 'LIKE')
  ->condition('uid.entity.status', 1)
  ->execute();

// Field example.
// If you need a simple check whether or not a particular field exists you can use the exists() or notExists() methods.
$query = \Drupal::entityQuery('commerce_product')
  ->condition('status', 1)
  ->condition('type', 'doc')
  ->notExists('field_tags')
  ->condition('field_doc_category', 134);

// Count example. 
$query = \Drupal::entityQuery('user');
$time = time();
$yesterday = $time - 60*60*24; 
$new_user_count = $query->condition('created', $yesterday, '>=')
  ->count()
  ->execute(); 

// Handy when building up the conditions of a query, orConditionGroup() and andConditionGroup(). 
// Either allows you to define a group of conditions which will subsequently be either OR'ed or AND'ed together.
$query = \Drupal::entityQuery('node'); 
$group = $query->orConditionGroup() 
  ->condition('uid', 22) 
  ->condition('uid', 14) 
  ->condition('uid.entity.name', 'admin');

$entity_ids = $query->condition('type', 'article') 
  ->condition($group) -
  ->execute();
<?php

$query = \Drupal::entityQuery('node')
    ->condition('status', 1)
    ->condition('changed', REQUEST_TIME, '<')
    ->condition('title', 'cat', 'CONTAINS')
    ->condition('field_tags.entity.name', 'cats');

$nids = $query->execute();

foreach ($nids as $nid) {
   
}
<?php
$nodes = \Drupal::entityTypeManager()
    ->getStorage('node')
    ->loadByProperties(['type' => ['article', 'page']]);
    
foreach ($nodes as $node) {
   
}
<?php

$name_taken = (bool) $this->entityQuery
  ->get('user')
  ->condition('name', $name)
  ->range(0, 1)
  ->count()
  ->execute();

$sorted_terms = $this->entityQuery
  ->get('taxonomy_term')
  ->sort('weight')
  ->sort('name')
  ->addTag('term_access')
  ->execute();

$entity_query = $this->queryFactory
  ->get('user')
  ->condition('uid', 0, '<>')
  ->pager(50);

$header = $this->buildHeader();
$entity_query->tableSort($header);
$uids = $entity_query->execute();