Even though Drupal 7 core fell short of a proper way of handling its brand new entity system (we currently rely on the great Entity module for that), it did give us EntityFieldQuery. For those of you who don’t know, EntityFieldQuery
is a very powerful querying class used to search Drupal entities programatically (nodes, users, etc).
It provides a number of methods that make it easy to query entities based on conditions such as field values or class properties. If you don’t know how it works, feel free to check out this documentation page or this great tutorial on the subject.
In this article I am going to talk about what we have in Drupal 8 for querying entities. There is no more EntityFieldQuery
, but there’s an entity.query
service that will instantiate a query object for a given entity type (and that implements the \Drupal\Core\Entity\Query\QueryInterface). We can access this service statically through the \Drupal namespace or using dependency injection.
First up, we’ll look at querying node entities and then we’ll see how to load them. The same techniques will work with other content entities as well (users, comments etc), but also with configuration entities, and that’s really cool.
The entity query service
As mentioned, there are two ways we can access the entity.query
service that we use for querying entities. Statically, we can do this:
Continue reading %The Drupal 8 version of EntityFieldQuery%
more
{ 0 comments... » The Drupal 8 version of EntityFieldQuery read them below or add one }
Post a Comment