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();

or

$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:
\Drupal::service(‘entity_type.manager’)->getStorage(‘node’)

For taxonomy, you would do:
\Drupal::service(‘entity_type.manager’)->getStorage(‘taxonomy_term’)

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”;

$node->save();

Delete article nodes having title “Lorem ipsum”:

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

\Drupal::service(‘entity_type.manager’)->getStorage(‘node’)->delete($nodes);

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’]);

Conclusion:

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
A Dive into Cybersecurity

A Dive into Cybersecurity

MAHMUDUL HASSAN
Custom Module Development in Magento 2

Magento 2 Custom Module Development

Akshay Naik
Overcoming Mistakes

Mistakes I made as a leader and my way of overcoming them

ARIF ISLAM