By chx on
Change record status:
Published (View all published change records)
Project:
Introduced in branch:
8.x
Issue links:
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:
entityConditionpreviously worked with entity id, revision id, bundle and entity_type. Now entity_type is passed to theentity_queryfactory function and the rest can be converted into property queries on the base table.propertyConditioncalls can be changed toconditioncalls without any change.fieldCondition($field_name, $property_name, $value, $operator)becomescondition("$field_name.$property_name", $value, $operator).fieldDeltaCondition($field_name, $value, $operator)calls now can be written asconditon("$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
Comments
Where does the $properties
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.
Ok, stupid question, I get it
Ok, stupid question, I get it.
($properties[..][..] is just a value, so it could be anything)
Is there some info about how
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.
Note that if you inject
Note that if you inject
entity.queryto your class, then change your code accordingly. This assumes that you set the service to$this->entityQuery.Global service
Dependency injection
It's different than the pattern with
\Drupal::config(), which threw me off.Support my open-source work: Patreon | Ko-Fi | FillPDF Service
Drupal Join Query with entity condition
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
Not all of this is actually supported yet
Worth noting that the following is actually dependent on #2384459: Add entity query condition for delta in EFQ which is still needs review:
Selecting bundles
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:
D8:
Access checks
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.
Access check entityQuery Drupal 8
I ended up with the same conclusions, entityQuery doesn't care if the current user can access the node (and probably entity too but I did not test that) and this is super annoying...