Loading entities with entity_type.managerfor Drupal 8 , 9 , 10 , and 11

Last updated :  

Introduction

In previous lessons, we learned about the basics of Drupal module development, focusing on how modules interact with the core structure. In this lesson, we'll dive deeper into understanding how to load Drupal entities effectively using the entity_type.manager service. Working with entities is a crucial part of Drupal development, and mastering this process will open up numerous possibilities in creating versatile and dynamic web applications.

Understanding Entities in Drupal

Before we begin, it is important to understand what entities are in the context of Drupal:

  • Entities are the building blocks of Drupal content, such as nodes, users, taxonomy terms, etc.
  • Each entity has a corresponding entity type (like 'node', 'user'), and within each entity type, there are multiple bundles (content types in the case of nodes).
Note: If you are familiar with Object Oriented Programming, you can think of an entity type as a class, and an entity as an instance of that class.

What is entity_type.manager?

The entity_type.manager service in Drupal acts as a bridge for interacting with entities. It provides methods to create, retrieve, update, and delete entities across various entity types.

How to Use entity_type.manager

To make use of entity_type.manager, you must first inject it into your service or controller. Here's how you can do it:

Injecting entity_type.manager

First, ensure your custom class can receive and handle service dependency injection:

namespace Drupal\custom_module\Controller;

use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;

class ExampleController extends ControllerBase {

  protected $entityTypeManager;

  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
    $this->entityTypeManager = $entity_type_manager;
  }

  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('entity_type.manager')
    );
  }

  public function loadNode($nid) {
    $node = $this->entityTypeManager->getStorage('node')->load($nid);
    return $node;
  }
}

Loading Entities

Loading a Single Entity

To load a single entity, you can use the load method from the specific storage handler of an entity type:

$node = $this->entityTypeManager->getStorage('node')->load(1);

In this example, we are loading a node entity with the ID of 1. Ensure the ID you are passing exists to avoid errors.

Loading Multiple Entities

Similarly, you can load multiple entities by using the loadMultiple method:

$nids = [1, 2, 3];
$nodes = $this->entityTypeManager->getStorage('node')->loadMultiple($nids);

This code snippet loads nodes with IDs 1, 2, and 3 into an associative array.

Practical Example

Imagine you're developing a custom module for a blog site, and you need to load articles authored by a specific user. You'd use a query to extract these criteria and then load the nodes:

$query = \Drupal::entityQuery('node')
  ->condition('type', 'article')
  ->condition('uid', $user_id)
  ->execute();

$articles = $this->entityTypeManager->getStorage('node')->loadMultiple($query);

Here, we're using entityQuery to filter nodes of type 'article' authored by the specified user ID, then loading them with loadMultiple().

Conclusion

Mastering entity management using entity_type.manager unlocks significant potential in your Drupal development journey, allowing you to create more responsive and dynamic modules. With the knowledge from this lesson, you can efficiently access and manipulate entities, laying the groundwork for advanced functionalities.

Teaser for the Next Lesson

In the upcoming lesson, we'll explore how to define custom entities with annotations. This will allow you to create uniquely tailored solutions to meet the specific needs of your Drupal projects. Stay tuned!