Using custom classes for entity bundles
Each entity is instantiated with its entity type class. A Node entity will be an instance of \Drupal\node\Entity\Node
and a Taxonomy Term entity will be an instance of \Drupal\taxonomy\Entity\Term
regardless of the entity’s bundle. Drupal allows altering entity information to provide alternative classes when an entity of a specific bundle is instantiated. Entity bundles are often used for different specific business logic. This feature allows the creation of classes with specific business logic associated with fields on the bundle.
In this recipe, we will create an entity bundle class to be used when there is a Recipe node.
Getting started
In this recipe, we will be using the Recipe content type provided by the Umami demo installation.
How to do it…
- First, we need to create the
src/Entity
directory in the module’s directory. This will translate to the\Drupal\mymodule\Entity
namespace, which is where we...