Module Development

Using Entity Bundle Classes for Site-Specific Features for Drupal 10, 11

Bundle classes are a feature of the Drupal Entity API that let you attach PHP classes to individual bundles (for example, content types) so you can keep site-specific business logic close to the data it acts on. Bundle classes are a powerful way to encapsulate site-specific business logic and bridge user-configured fields and PHP logic based on those fields. In this tutorial, you will build a reusable bundle base class that adds a generic share behavior for all node types on a site and then extend it for a specific node type.

In this tutorial, we will:

  • Explore a real-world problem that bundle classes can help you solve.
  • Create a bundle base class for common share features shared across multiple node types.
  • Implement a node-type-specific class that extends the base class with behavior tailored to the underlying content type.

By the end of this tutorial, you'll be able to use bundle classes to encapsulate per-bundle logic, assume the presence of UI-configured fields when appropriate, and register those classes so Drupal uses them at runtime.