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.

Learn DrupalDrupal glossary

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:

  • id and label: 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_type or bundle_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 typeSubtype (bundle)
Content item (node)Content type
CommentComment type
Taxonomy termVocabulary
Content blockBlock type
User profile, fileNone

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

Questions about Entity type

The entity type is the broad kind, such as node or taxonomy term. A bundle is a variant inside it, such as the Article content type or a Tags vocabulary. All bundles of one entity type share its storage and handlers; each bundle gets its own fields.

A content entity type holds user-facing data such as nodes, terms, users, comments and files. Its entities are built from fields and can have revisions and translations. A configuration entity type holds settings, such as content types and user roles, that can be exported and imported between environments.

In a class in the src/Entity folder of a module, marked with the ContentEntityType or ConfigEntityType attribute. The attribute carries the ID, label, keys, handlers and links. The ID can have at most 32 characters.

Related terms

All Drupal terms from A to Z