Change record status: 
Project: 
Introduced in branch: 
8.4.x
Introduced in version: 
8.4.1
Description: 

In previous versions of Drupal 8, entity queries were able to target either the default revision or all revisions of an entity type. Starting with Drupal 8.4.1, it is now possible to target only the latest revisions of an entity type.

Theoretical example

Given this revision history of a node:

- v1: initial revision
- v2: default revision (the one that's usually displayed to the site users)
- v3: pending revision (a work-in-progress revision that is not yet ready to be shown to the site users)

Before Drupal 8.4.1, an entity query could only target either v2 or all of them at once (v1, v2 and v3). Now you can also query only v3 by using the new latestRevision() method from the entity query interface.

Code example

Writing a query that targets the latest revisions is simply a matter of adding a latestRevision() call to an entity query, like this:

\Drupal::entityTypeManager()->getStorage('<entity_type_id>')->getQuery()
  ->latestRevision()
  ->condition('<field_name>', '<field_value>', '=')
  ->execute();

Considering the theoretical example above, this query will only search for the given value of a field on v3, the latest revision of the node.

Impacts: 
Module developers

Comments

leksat’s picture

In this theoretical example, if I call latestRevision(), the query will return v3 (pending revision).
But from this change record it's not quite clear what's the default behavior. Will it return v2 (default revision) in case of no latestRevision() call?

plach’s picture

Yes, the default behavior is returning the default revision and that as not changed.