8.2.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.3.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
BookNavigationBlock::build in core/modules/book/src/Plugin/Block/BookNavigationBlock.php
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.