How to Create a Custom Plugin in Drupal Using Attribute-Based Plugin System
Jo Fitzgerald’s detailed tutorial walks developers through the steps to build a custom plugin in Drupal using modern, attribute-based methods introduced in Drupal 10.2. The article explains the five required components of a plugin system: an interface, an optional base class, a PHP attribute for metadata, a plugin manager class, and the actual plugin implementation. It emphasises best practices, including defining interfaces for consistent behaviour and using base classes to share functionality.
The guide highlights how plugins are now defined using PHP attributes, replacing older annotation-based approaches. Jo details how to register the plugin manager as a service and how to implement the manager class by extending DefaultPluginManager. An example plugin is provided, showing how to use a custom attribute to declare plugin metadata like ID and label. The post also covers how to instantiate and use the plugin via the service container or dependency injection.
Jo references real-world projects—such as Token Modifier, Metatag Link Preview, and a proposed improvement to Markdown Easy—to illustrate the power and flexibility of Drupal’s plugin architecture. The article sets the stage for a follow-up post on using Drush Generate to streamline plugin manager creation.

