Entity CRUD in Drupal 8

Manipulate Drupal Entities With Entity API


What is an entity?

An entity is a basic building block. It represents a piece of content that a user interacts with. For e.g. nodes, blocks, taxonomies, users, etc. are all examples of entities in Drupal 8.

Entities can be classified as:

  • Configuration entities
  • Content entities

Configuration entities store configuration settings. Image styles, block or views settings, user roles, etc. are some examples of configurations. They do not store data but rather how data should be manipulated or represented.

On the other hand, content entities store content. They are the main building blocks for data modeling. Nodes, blocks, users, etc. are some examples of content entities.

We use the EntityTypeManager service to handle entity CRUD operations. Dependency injection is the preferred method to use to obtain an instance of the EntityTypeManager object as follows:

$entityTypeManager = \Drupal::entityTypeManager();


$entityTypeManager = \Drupal::service(‘entity_type.manager’);

We can then access the storage controller and call the required CRUD actions on the entity.

The CRUD actions would be: createloadsave and delete.

Here we will show examples of how to perform CRUD operations on the “node” entity. You can perform similar operations on other Drupal entities too.

After, obtaining an instance of the EntityTypeManager object, we need to get access to the entity’s storage controller. We do this, by specifying an entity type as an argument to the getStorage API.

For example, to get access to the node storage controller, you would call the getStorage API as:

For taxonomy, you would do:

Here is a complete list of entities in Drupal: https://www.drupal.org/files/classDrupal_Entities.png

To determine the entity type passed as the argument to the getStorage() API, simply open the entity code file and look for the id annotation key in the @EntityType annotation.

For example, to determine the entity type for taxonomy term, open the file at core/modules/taxonomy/src/Entity/Term.php and find the value of the id annotation key for @ContentEntityType annotation.

Entity CRUD in Drupal 8

Now you can call any of the entity CRUD operations on the storage controller.

Below are some examples of entity CRUD operations:

Create a new node of type “article”:

$article = \Drupal::service(‘entity_type.manager’)->getStorage(‘node’)->create([‘type’ => ‘article’, ‘title’ => ‘Lorem Ipsum Article’]);

Load all unpublished “article” nodes:

$unpublished_articles = \Drupal::service(‘entity_type.manager’)->getStorage(‘node’)->loadByProperties([‘type’ => ‘article’, ‘status’ => 0]);

Update the title of a node having ID 123:

$node = \Drupal::service(‘entity_type.manager’)->getStorage(‘node’)->load(123);

$node->title = “Lorem ipsum dolor sit amet”;


Delete article nodes having title “Lorem ipsum”:

$nodes = \Drupal::service(‘entity_type.manager’)->getStorage(‘node’)->loadByProperties([‘type’ => ‘article’, ‘title’ => ‘Lorem ipsum’]);


Load a taxonomy term having id 123:

$term = \Drupal::service(‘entity_type.manager’)->getStorage(‘taxonomy_term’)->load(123);

Get all terms from a vocabulary named “glossary”:

$terms = \Drupal::service(‘entity_type.manager’)->getStorage(‘taxonomy_term’)->loadByProperties([‘vid’ => ‘glossary’]);


The Entity API makes it easy to programmatically manipulate Drupal entities and it provides a unified method of working with them.

Jose D'silva
Jose D'silva
Senior Software Engineer
Why Automation Testing Cannot Completely Replace Manual Testing

Why Automation Testing Cannot Completely Replace Manual Testing

Anish Miranda
Why Project Manager Meetings are Important

The Significance and Benefits of Project Manager Meetings

Viktor Kalinchuk
Earth Hour

Earth Hour 2014 Saturday, March 29 8:30 PM- 9:30 PM