If you write custom Drupal 8 (or 9) modules, then you've probably used the entity QueryInterface - which is accessible from the Drupal::entityQuery() static method. If not, then you're missing out on this incredibly useful tool that allows you to easily query a Drupal site for entities of any kind.
For example, if you are looking for all nodes of type Movie that have a Release year of 2009, you can do something like this:
$result = \Drupal::entityQuery('node') ->condition('type', 'movie') ->condition('field_release_year', '2009') ->execute();
But what if you want to base the condition on a value of a referenced entity? Maybe you want to find all nodes of type Movie where the director of the movie was born in 1981? Assume nodes of type Movie have an entity reference field to nodes of type Director, where one of the fields on the Director content type was Birth year. It's almost as easy to write an entityQuery condition for this situation as well:
$result = \Drupal::entityQuery('node') ->condition('type', 'movie') ->condition('field_director.entity:node.field_birth_year', '1981') ->execute();
Note the entity:node bit in the second condition - this is what allows you to access fields in the referenced entity.
Brilliant tip, thanks very much. I'll definitely use this!
That's very cool.
Is there a way to get a value for the reverse reference field in a similar way?
Tim - that's a great question that I do not have the answer to. I suspect the answer is "no", but I don't have any hard evidence to back that up.