Building queries

Now that we have an entity query factory on our hands, we can build a query that is made up of conditions and all sorts of typical query elements. Here's a simple example of querying for the last 10 published article nodes:

$query      ->condition('type', 'article')      ->condition('status', TRUE)      ->range(0, 10)      ->sort('created', 'DESC');$ids = $query->execute();

The first thing you can see is that the methods on the factory are chainable. We have some expected methods to set conditions, range, sorting, and so on. I strongly recommend you check out the QueryInterface class for some documentation about these methods, especially the condition() method which is the most complex. As you can already deduce, the first parameter is the field ...

Get Drupal 8 Module Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.