Drupal's Entity API

In order to really get a grasp on Drupal, especially how Drupal models data, it's necessary to have at least a familiarity with the Entity API. If you're already familiar with the Entity system in Drupal 7, you may be interested in starting with our What's new in Drupal 8 with the Entity Field API video. If you're new to Drupal, don't sweat. The tutorials in this series assume you're starting from scratch.

We start off with an overview of the terminology associated with the Entity system. We then look at a high level overview to see how the various components come together to form an API. Our configuration management series already has tutorials on Configuration Entities, but in this series we introduce how to construct your own custom content entity. We will also take time to learn how entities can be created, read, updated and deleted programmatically from elsewhere in our code as well as how to use the Entity query service to identify entities matching particular conditions.

This series also includes tutorials on the Entity systems' other hooks, and more about how you can interact with existing entity implementations.

Tutorials in this course
Categories
Drupal 8, 9, and 10
More information

When learning Drupal development, it won't be long before you encounter the word "entity" and the Entity API. But what are entities in Drupal? How can you use them to build your site? When should you use the Entity API?

This tutorial will explain Drupal's Entity system from a high level. We'll look at:

  • The main problems the Entity system solves
  • Key terms you should know
  • Key concepts we'll explore as we dive into Drupal's Entity API

By the end of this tutorial you should be able to explain the problems that the Entity API solves, and when you should use it in your own code.

More information

Entities are the building blocks that make up just about everything on a Drupal site. Regardless of whether entities provide configuration information or content, they are absolutely crucial to Drupal's data model.

In this tutorial we'll:

  • Look at the overall class hierarchy between the various entity types to see how they're related.
  • Examine differences between configuration and content entities in the code that defines them.
  • Look at some of the core code required to create an entity type.

By the end of this lesson you should be able to use an example to create a custom entity type in code.

Categories
Drupal 8, 9, and 10
More information

The Drupal Entity API makes it easy to define new custom content entity types. And in most cases whenever you want to store custom content in Drupal you should use the Entity API instead of making CRUD queries directly to the database. This ensures your code is more consistent, and allows you to take advantage of all the features of the Entity API like access control, Views module integration, and automatic JSON:API support. As well as making it easier for others to extend your custom content by ensuring all the proper hooks and lifecycle events get invoked when new content items get created, updated, and deleted.

In this tutorial we'll:

  • Walk through the process of creating a custom content entity

By the end of this lesson you'll be able to create your own custom content entity contained in a module.

More information

Code generators are great productivity boosters that allow generating scaffolds for common development tasks in Drupal. One of the most common use cases for generators is scaffolding the code required for a custom entity type. Custom entities require many files and complicated annotations in order to function properly. There is a lot of boilerplate code that is more-or-less the same for every entity type. Creating all the files is repetitive, time-consuming, and prone to human error. Generators can help automate this task and make creating your own custom entity types quicker.

In this tutorial we'll:

  • Learn how to generate the code for a custom entity with Drush
  • Learn about the options that generators provide for custom entities

By the end of this tutorial you should know how to generate custom entities with Drush.

Categories
Drupal 8, 9, and 10
More information

Entity CRUD (Create, Read, Update, and Delete) operations are handled via the EntityTypeManager service.

In this tutorial we'll:

  • Learn how to use the EntityTypeManager service to perform basic CRUD operations with examples you can copy/paste
  • Access both property and field values of an entity
  • Update entities by setting new field values and then saving the object

By the end of this tutorial, you'll be able to understand Entity CRUD operations and be well on your way to becoming comfortable with accessing and manipulating entity values in code.

Categories
Drupal 8, 9, and 10
More information

Often when building a site in Drupal you'll find yourself wanting to display a list of nodes, or find entities created by a particular author, or locate some content based on a particular set of criteria. Rather than querying the database directly, Drupal provides a helper class, EntityQuery, to make things a bit easier. The EntityQuery class will probably look very familiar to you if you're familiar with the Database API, or the EntityFieldQuery class in Drupal 7.

In this tutorial we'll:

  • Go through several examples of using EntityQuery to find subsets of content.
  • Demonstrate how to iterate over the results of an EntityQuery query.

By the end of this tutorial, you should understand how to use entity queries to create custom sets of data from entities.

Categories
Drupal 8, 9, and 10
More information

Drupal's Entity system provides several hooks that allow custom code to interact with various parts of the entity life cycle.

In this tutorial we'll:

  • Examine the available hooks
  • Learn how to make use of them to act on several different types of operations on individual entities

By the end if this tutorial you should have a better understanding of the hooks available to developers who want to respond to entity lifecycle operations and how to use them to customize the way specific entity types work.

More information

On occasion you may need to modify the behavior of entity types defined by another module. Thankfully Drupal includes several alter hooks that can be used to override the behavior of another entity.

In this tutorial we will:

  • Walk through the common Entity API hooks
  • Look at example implementations of each
  • And discuss the use cases for each

By the end of this tutorial you will have a better understanding of how to override the default behavior of an entity type provided by Drupal core (or another contributed module) within your custom code.

Categories
Drupal 8, 9, and 10
More information

Drupal includes the Symfony Validator component, and provides an Entity Validation API to assist in validating the values of fields on an entity. By using the Entity Validation API you can ensure that you're validation logic is applied to Entity CRUD operations regardless of how they are triggered. Whether editing an Entity via a Form API form, or creating a new Entity via the REST API, the same validation code will be used.

Using the Entity Validation API in order to validate the value of a field on an entity requires:

  • Defining (or choosing) a constraint plugin
  • Defining (or choosing) a validation plugin
  • Adding the constraint to the field definition

In this tutorial, we'll look at how this Validation API works and how it can be used in custom code to ensure our entities have properly constructed values. We'll look at how this validation works in Drupal core and how we can add our own additional constraints. We'll also see how to work with the error messages returned from the validator when our entity doesn't pass validation.

Categories
Drupal 8, 9, and 10
More information

One of Drupal's more powerful features is the fine-grained ability to control permissions and control access to content. The Entity API helps enable this functionality by providing an interface to help define access control.

In this tutorial we'll:

  • Look at how access control is handled, using Drupal core as an example.
  • Demonstrate how to implement access control in a custom module.
  • Learn about the hooks that allow developers to modify access control for entities provided by another module.

By the end of this tutorial you should have a better understanding of the entity access control system and how to work with it.

Categories
Drupal 8, 9, and 10
More information

The Typed Data API in Drupal helps add additional functionality to PHP's built-in data types that make working with data in Drupal much more predictable. It allows code to make intelligent guesses about the type of data that a field on an entity contains. For example differentiating between a string of text, and a string of text that represents a URL.

In this tutorial we'll:

  • Look at the 3 main types of typed data in Drupal: primitives, complex data, and lists.
  • See how different data types and definitions are defined and show how to define your own data type.
  • Look at the interfaces provided by each data type to see some of the benefits to adding this abstraction layer.

By the end of this tutorial you should have a better understanding of what the Typed Data API is, where you'll most likely encounter it, and how to use it in your code.

This course appears in the following guides:
Categories
Module Development, Backend and Infrastructure
Drupal 7, 8, 9, and 10