8.3.x Drupal.php public static Drupal::entityQuery($entity_type, $conjunction = 'AND')
8.0.x Drupal.php public static Drupal::entityQuery($entity_type, $conjunction = 'AND')
8.1.x Drupal.php public static Drupal::entityQuery($entity_type, $conjunction = 'AND')
8.2.x Drupal.php public static Drupal::entityQuery($entity_type, $conjunction = 'AND')
8.4.x Drupal.php public static 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.

60 calls to Drupal::entityQuery()
aggregator_cron in core/modules/aggregator/aggregator.module
Implements hook_cron().
BookManager::bookTreeCheckAccess in core/modules/book/src/BookManager.php
Checks access and performs dynamic operations for each link in the tree.
BookNavigationBlock::build in core/modules/book/src/Plugin/Block/BookNavigationBlock.php
Builds and returns the renderable array for this block plugin.
BulkDeleteTest::testPurgeField in core/modules/field/tests/src/Kernel/BulkDeleteTest.php
Verify that field data items and fields are purged when a field storage is deleted.
CommentLanguageTest::testCommentLanguage in core/modules/comment/src/Tests/CommentLanguageTest.php
Test that comment language is properly set.

... See full list

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.

rondog479’s picture

You want to do

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

docs link

Jodan’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);
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();