Entity type
An entity type is a kind of entity a Drupal module defines, such as node, user or taxonomy term, with its own class, storage and handlers.
An entity type is a kind of entity that a module defines for Drupal, such as node, user, taxonomy term or file. It names the PHP class for its entities and the handlers for storage, forms, access and display. Entity types are either content entity types, for site data, or configuration entity types, for settings. Most content entity types are divided into bundles.
How entity types work in Drupal 11
Each entity type is a plugin. The class sits in the Entity namespace of its module and carries an attribute: ContentEntityType or ConfigEntityType, both built on the base attribute EntityType. The attribute sets the values that define the type:
idandlabel: the machine name and the human name.entity_keys: which property is the ID, the label, the UUID and the bundle.handlers: the classes for storage, forms, access, list builder and so on.links: the address templates, such as/node/{node}/edit.bundle_entity_typeorbundle_of: how the type connects to its bundles.
Content entity types keep their data in their own database tables. Configuration entity types use the shared config table, according to Drupal.org. The entity type manager reads the definitions: getDefinition('node') returns one and getDefinitions() returns all of them, keyed by ID. Modules can add to or change definitions with hook_entity_type_build() and hook_entity_type_alter().
The user guide lists the common content entity types and what they split into; Drupal 11 calls the custom block a content block:
| Entity type | Subtype (bundle) |
|---|---|
| Content item (node) | Content type |
| Comment | Comment type |
| Taxonomy term | Vocabulary |
| Content block | Block type |
| User profile, file | None |
Example
Ask the entity type manager about the node entity type:
$definition = $entityTypeManager->getDefinition('node');
$definition->getBundleEntityType(); // 'node_type'
$definition->getBaseTable(); // 'node'
The first call returns the ID of the entity type that supplies the bundles, which is node_type, the entity type of the content types themselves. A content type is therefore an entity of one entity type (node_type) and a bundle of another (node).
Common mistakes
- Using entity type and bundle as synonyms. A field is added to a bundle, not to the whole entity type, unless it is a base field defined in code.
- Choosing a long machine name. The Entity API topic asks for an entity type machine name of 32 characters or fewer.
- Defining a custom entity type for plain content. A new bundle of an existing type needs only configuration. A new entity type needs a class, an interface, handlers and routes, as the Entity API topic lists.
For marketing and content teams
If a developer proposes a new content type, you get a new template for the existing content item type, which you set up under Structure › Content types. A new entity type is custom code in a module. Ask which of the two is meant before you plan a launch around it.
Sources
- Drupal.org: Entity types
- Drupal.org: Introduction to the Entity API in Drupal
- Entity API topic, Drupal API
- Drupal User Guide: Concept: Content entities and fields
- Drupal User Guide: Glossary
- EntityTypeInterface, Drupal API
- EntityTypeManagerInterface, Drupal API
- Node entity class, Drupal core 11.x
- ContentEntityType attribute, Drupal core 11.x
- BlockContent entity class, Drupal API