Change record status: 
Project: 
Introduced in branch: 
8.x
Description: 

Drupal 7:

    $query = new EntityFieldQuery();
    $query->entityCondition('entity_type', 'entity_test');
    $query->entityCondition('langcode', $default_langcode);
    $query->propertyCondition('user_id', $properties[$default_langcode]['user_id'], NULL, 'original');
    $query->propertyCondition('name', $properties[$default_langcode]['name'], NULL, 'original');
    $query->propertyLanguageCondition($default_langcode, NULL, 'original');
    $query->propertyCondition('name', $properties[$langcode]['name'], NULL, 'translation');
    $query->propertyLanguageCondition($langcode, NULL, 'translation');
    $query->fieldCondition($this->field_name, 'value', $field_value, NULL, NULL, 'translation');
    $query->fieldLanguageCondition($this->field_name, $langcode, NULL, NULL, 'translation');
    $query->propertyOrderBy('name', 'ASC', 'original');
    $result = $query->execute();

And $result was a multidimensional array keyed by entity type and entity id with values of pseudo-entities containing id, revision and bundle.

Drupal 8:

    $query = \Drupal::entityQuery('entity_test');
    $default_langcode_group = $query->andConditionGroup()
      ->condition('user_id', $properties[$default_langcode]['user_id'], '=', $default_langcode)
      ->condition('name', $properties[$default_langcode]['name'], '=', $default_langcode);
    $langcode_group = $query->andConditionGroup()
      ->condition('name', $properties[$langcode]['name'], '=', $langcode)
      ->condition("$this->field_name.value", $field_value, '=', $langcode);
    $result = $query
      ->condition('langcode', $default_langcode)
      ->condition($default_langcode_group)
      ->condition($langcode_group)
      ->sort('name', 'ASC', $default_langcode)
      ->execute();

The query now only works on a single entity type.

The result is an array keyed by revision id if the entity type supports revisions and entity id if not. The values are always entity IDs. This allows the result array to be passed to entity_load_multiple.

To convert a query:

  • entityCondition previously worked with entity id, revision id, bundle and entity_type. Now entity_type is passed to the entity_query factory function and the rest can be converted into property queries on the base table.
  • propertyCondition calls can be changed to condition calls without any change.
  • fieldCondition($field_name, $property_name, $value, $operator) becomes condition("$field_name.$property_name", $value, $operator).
  • fieldDeltaCondition($field_name, $value, $operator) calls now can be written as conditon("$field_name.%delta", $value, $operator). Also, delta values can be added to any condition: condition("$field_name.$delta.$property_name", $value, $operator) where $delta is a numeric value.

Same for sorts. Delta and language grouping is deprecated in favor of simply creating andConditionGroup orConditionGroup.

Impacts: 
Module developers
Updates Done (doc team, etc.)
Online documentation: 
Not done
Theming guide: 
Not done
Module developer documentation: 
Not done
Examples project: 
Not done
Coder Review: 
Not done
Coder Upgrade: 
Not done
Other: 
Other updates done

Comments

donquixote’s picture

Where does the $properties array come from?
I would like to create an example entity query in D8, but with the $properties unexplained I can't simply copy from here.

donquixote’s picture

Ok, stupid question, I get it.
($properties[..][..] is just a value, so it could be anything)

ivanjaros’s picture

Is there some info about how to properly query for the right translation? I don't get the default language and language with 'original' and 'translation' stuff.

wizonesolutions’s picture

Note that if you inject entity.query to your class, then change your code accordingly. This assumes that you set the service to $this->entityQuery.

Global service

$query = \Drupal::entityQuery('entity_test');

Dependency injection

$query = $this->entityQuery->get('entity_test');

It's different than the pattern with \Drupal::config(), which threw me off.

FillPDF Service - http://fillpdf-service.com - Hosted solution for FillPDF

sharmagaurav458’s picture

I have two content type A and B in which B content type node is reference in A content type node. In B content type node there is status field. I want to fetch the A content type node in which the status of B content type node is something.

Can you please guide me how can I use this?

Thanks

andrewbelcher’s picture

Worth noting that the following is actually dependent on #2384459: Add entity query condition for delta in EFQ which is still needs review:

fieldDeltaCondition($field_name, $value, $operator) calls now can be written as conditon("$field_name.?", $value, $operator). Also, delta values can be added to any condition: condition("$field_name.$delta.$property_name", $value, $operator) where $delta is a numeric value.

Ashley George’s picture

It looks like you need to swap 'bundle' for 'type' when you want to pull all nodes of a certain content type, for example.

D7:

$query->entityCondition('bundle', 'article');

D8:

$query->condition('type', 'article');
Graber’s picture

In Drupal 7 entities are queried with access checks with the current user as default account. I did some tests on different users and it seems that Drupal 8 entityQuery has no access checks implemented.