8.5.x Drupal.php Drupal::entityQuery($entity_type, $conjunction = 'AND')
8.0.x Drupal.php Drupal::entityQuery($entity_type, $conjunction = 'AND')
8.1.x Drupal.php Drupal::entityQuery($entity_type, $conjunction = 'AND')
8.2.x Drupal.php Drupal::entityQuery($entity_type, $conjunction = 'AND')
8.3.x Drupal.php Drupal::entityQuery($entity_type, $conjunction = 'AND')
8.4.x Drupal.php Drupal::entityQuery($entity_type, $conjunction = 'AND')
8.6.x Drupal.php Drupal::entityQuery($entity_type, $conjunction = 'AND')

Returns the entity query object for this entity type.

Parameters

string $entity_type: The entity type (for example, node) for which the query object should be returned.

string $conjunction: (optional) Either 'AND' if all conditions in the query need to apply, or 'OR' if any of them is sufficient. Defaults to 'AND'.

Return value

\Drupal\Core\Entity\Query\QueryInterface The query object that can query the given entity type.

File

core/lib/Drupal.php, line 435
Contains \Drupal.

Class

Drupal
Static Service Container wrapper.

Code

public static function entityQuery($entity_type, $conjunction = 'AND') {
  return static::getContainer()
    ->get('entity.query')
    ->get($entity_type, $conjunction);
}

Comments

taggartj’s picture

   $bundle = $node->bundle(); // or $bundle='my_bundle_type';
   $query = \Drupal::entityQuery('node');
    $query->condition('status', 1);
    $query->condition('type', $bundle);
    $entity_ids = $query->execute();
taggartj’s picture

$query = Drupal::service('entity.query')
      ->get('node')
      ->condition('field_something', 'some_value');
 $entity_ids = $query->execute();
taggartj’s picture

use Drupal\Core\Entity\Query\QueryFactory;

//class someController extends ControllerBase {
/**
   * Drupal\Core\Entity\Query\QueryFactory definition.
   *
   * @var Drupal\Core\Entity\Query\QueryFactory
   */
  protected $entityQuery;

  public function __construct(QueryFactory $entityQuery) {
    $this->entityQuery = $entityQuery;
  }

public static function create(ContainerInterface $container) {
    return new static(
      $container->get('entity.query')
    );
  }

function someFunction() {
  //
    $query = $this->entityQuery->get('entity_type');
    $query->condition('field', $id);
    $entity_ids = $query->execute();
  //
}

also can pass via service
my_service_that_needs_entity_query:
class: ' Drupal\my_module\SomeClass'
arguments: ['@current_user', '@entity.query']

mradcliffe’s picture

The get method for entity query factory takes an EntityTypeInterface object, not a string.

Edit: The API documentation is fairly inconsistent. On QueryFactory::get, it is a string, but on QueryFactoryInterface::get it is a EntityTypeInterface. But QueryFactory doesn't inherit from QueryFactoryInterface making it really confusing to search for things and pick out the right thing.

adominique’s picture

Can $entityQuery->condition() be extended with 'LIKE' ?

i.e. condition('settings.id', 'idname%', 'like')

adominique’s picture

Ok, found operator 'STARTS_WITH' which is available.
Thank you.

pownraj’s picture

How to check for value null?

Here I tried to check if the comment doesn't have any parent but no luck

$comment_count = \Drupal::entityQuery('comment')
    ->condition('entity_id', $nid)
    ->condition('entity_type', 'node')
    ->condition('pid', NULL) /* Here I have used null but it isn't working */
    ->count()->execute();
Graber’s picture

https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Database%...

Hmmm.

->condition('pid', NULL, 'IS NULL')

But it may be a bit more complicated.

rondog469’s picture

You want to do

->notExists('pid'). There is also exists()

docs link

Pascal-’s picture

field_company is a custom user field Entity reference, that refers to a node
$nid is the node id I want to filter on

    $ids = \Drupal::entityQuery('user')
      ->condition('status', 1)
      ->condition('field_company', $nid)
      ->execute();
    $users = User::loadMultiple($ids);
    kint ($users);

You can also use this to filter by role:

    $>condition('roles', 'administrator')
helioha’s picture

I'm trying to query a node of a specific type but it is returning nothing because my migrating plugin doesn't have access to that type of node. Is there any way to authenticate or bypass permissions before the query?

dmurphy1’s picture

helioha - I believe you can use the accessCheck method. For example, this worked for me:

    $query = \Drupal::entityQuery('node')
      ->condition('status', 1)
      ->condition('type', 'your_content_type')
      ->accessCheck(FALSE);
helioha’s picture

🏆🏆🏆

sebby’s picture

Thank you it's very helpful

pawel_r’s picture

$query = \Drupal::entityQuery('node');
$query->condition('status', 1);
$query->condition('type', 'MY_NODE_TYPE');

$query->condition('langcode', $language()->getId());

$ids = $query->execute();

Language can be obtained from other node, i.e.:
$language = $node->language();
or language manager:
$language = \Drupal::languageManager()->getCurrentLanguage();

jimmy_sebastian’s picture

$bundle = 'emails';
$query = \Drupal::entityQuery('notification');
$query->condition('status', 1);
$query->condition('type', $bundle);
$query->condition('field_name.value', 'default', '=');
$entity_ids = $query->execute();

sriharsha.uppuluri’s picture

In entity query How to get field values in results?

jimmy_sebastian’s picture

lykyd’s picture

$ids = \Drupal::entityQuery('block')
  # by block name
  ->condition('plugin', 'help_block')
  # by contrib module name that provides the block
  ->condition('settings.provider', 'facets')
  # by region
  ->condition('region', 'header')
  # by status
  ->condition('status', 1)
  ->execute();
GiorgosK’s picture

Is entityQuery cached ?