Loading entities

Now that we have our entity IDs found by the query, it's time to load them. It couldn't be simpler to do so. We just use the storage handler for that entity type (and we get that from the entity type manager):

$nodes = \Drupal::entityTypeManager()->getStorage('node')->loadMultiple($ids);

This will return an array of EntityInterface objects (in this case NodeInterface) or if we have only one ID to load:

$nodes = \Drupal::entityTypeManager()->getStorage('node')->load($id);

These will return a single NodeInterface object.

The Entity type storage handler also has a shortcut method that allows you to perform simple queries and load the resulting entities in one go. For example, if we wanted to load all article nodes:

$nodes = ...

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.