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

  • Good SEO Plus Lazy Marketing Won’t Cut It Anymore

    SEO is a playground for creative, scrappy problem-solvers. But today, it’s often reduced to a formula: plug keywords into an AI tool. Run a cookie-cutter checklist to create content. Build links. Hope traffic rolls in.  That used to work. But with AI-powered search on the rise, the era of SEOs as technicians is fading, and the…

  • 8 Best Free Security WordPress Plugins — Speckyboy

    Keeping your website secure is a 24/7 job. The right tools help keep watch – even when you can’t. They could be the difference between a hacked site and business as usual. WordPress security plugins are one part of that equation. Along with quality hosting and users practicing secure habits, a plugin can thwart common…

  • 9 Best Dedicated Server in India 2026, February🇮🇳Top Picked

    Have you ever struggled with controlling your server environment? Or found yourself frustrated due to unexpected downtimes and server crashes, especially during malicious traffic attacks that you never saw coming? These challenges are common when you’re relying on low-quality hosting services that can’t handle sudden spikes. That’s where Best Dedicated Server Hosting in India steps…

  • Understanding and troubleshooting failed orders

    Running an online store means making sure every part of the shopping experience goes smoothly.  So when an order fails on your site, it can be frustrating and confusing for both you and your customer. Is there something wrong with your store? Did the customer fill something out incorrectly? What steps should you take next?…

  • 2 New Tutorials Added to Single Directory Components Course

    As promised, we’ve added 2 new tutorials to our course, Single Directory Components in Drupal: Props and slots are both mechanisms for passing data and content to UI components. In Understanding Props and Slots in Drupal Single Directory Components, you’ll learn the difference between props and slots in Drupal SDCs, and how to choose the…