Entity API Course Updated to Cover the Use of PHP Attributes

As of Drupal 11.1.0, core has support for defining custom entity types (both content and configuration) using PHP attributes instead of annotations. And now, all our Entity API tutorials have been updated to reflect this change.

We previously performed extensive updates that rewrote all our tutorials that made use of annotations for Drupal plugins. See Keeping up with Drupal’s Evolving Plugin API: Updating Tutorials for PHP Attributes for more about those changes. But at that time, core did not yet support the use of PHP attributes for content or configuration entity type definitions. It does now, though!

What changed

At a high level, the metadata for custom content and configuration types is declared using PHP attributes instead of annotations. In your code, you’ll now author a #[ContentEntityType] or a #[ConfigEntityType] attribute, instead of a @ContentEntityType or @ConfigEntityType annotation, to provide details like the entity type’s id and label, and to configure handlers[], access control, and routing.

Seven tutorials in our Entity API course were rewritten and polished, and there’s a brand-new properties reference that dives deep into all the available #[ContentEntityType] attributes’ properties, what they do, and when to use them. In our research, we found that figuring this out still required quite a bit of code spelunking, so we wanted to surface as much of the information we found as we can. This new tutorial includes usage snippets that you can copy/paste, walking you through default values, types, and common pitfalls for identification keys, handlers, routing metadata, forms, Views integration, cache contexts, and nested arrays, such as links[] and handlers[].

We also took this opportunity to refresh the Entity API Implementation Basics tutorial based on feedback from members that said it was missing some critical information and was confusing. It now explains why you’d created a custom entity type in the first place, the differences between configuration and content entities, and the previous base-field code sample has been trimmed to its essentials to keep the focus on concepts. We also introduced bundle classes (and have another tutorial about these in the works), and updated the prerequisite of PHP attributes so the terminology stays consistent.

Should I use PHP attributes or annotations?

While annotations are still supported via a backwards compatibility layer, if you’re starting a new project, you should now use PHP attributes. We expect support for annotations will be dropped sometime in the future. Though not until Drupal 12, if not later.

The caveat to this is if you need your module to be compatible with versions of Drupal core before 11.1.0. In which case, you’ll still need to use annotations to define your custom entity types. Luckily, all the key/value pairs are still the same; it’s really just the syntax that’s different. So things like the new ContentEntityType Attribute: Properties Reference will still be useful.

Enjoy the updates, and please let us know if you have any questions about the move from annotations to PHP attributes.

Similar Posts

  • Release Day: The Drupal Recipes API

    We recently completed a new set of tutorials covering the Drupal Recipe API. My interest in Drupal recipes began while I was working on documentation for the Drupal CMS User Guide, which relies heavily on recipes to provide its features. Drupal CMS is just Drupal core plus a curated set of contributed modules, preconfigured to…

  • Best dropshipping products and ideas for 2026 and beyond

    Whether you’re just starting a new dropshipping business or growing an existing one, success is all about the products and how you market them. You don’t need to deal with shipping, inventory, or packaging — all you have to do is find trending products that eager customers will buy at profitable prices that you can…

  • Accelerate Sustainability Targets with VMware

    Hosting providers are becoming more competitive in their electricity consumption. This is measured by the amount of data centre power consumption versus how much of the total Kwh is being used by the actual computing power of the data centre, known as the PUE (Power Usage Effectiveness). At a recent customer supplier day I learnt…

  • 10 Best Hostinger Alternative 2025, Sep | Top Competitors

    When it comes to affordable web hosting services, Hostinger is often one of the first names that pops up. It’s budget-friendly, beginner-friendly, and offers a simple platform for individuals starting their online journey.  However, when it comes to handling enterprise-level workloads that need powerful hosting solutions, the question comes, Can Hostinger provide big dedicated solutions?…