Date:
15 February 2024

The main focus for this month’s Melbourne Drupal Meetup was a presentation by Lee Rowlands. Lee shared some of the latest developments in Drupal 10.3+

Plugin annotations moving to plugin attributes: presentation summary

This presentation offers insights into a significant shift in Drupal 10.3+External Link : the transition from plugin annotations to PHP attributes. This change, driven by the introduction of native PHP attributes in PHP 8, aims to improve the developer experience and streamline the management of plugins in Drupal.

The evolution of plugin annotations

Historically, Drupal used the Doctrine Simple Annotation Reader to manage plugin annotations. This system allowed developers to reference annotations without fully qualifying class names, which was intended to simplify development but led to potential conflicts and maintenance challenges. As PHP evolved, particularly with the release of PHP 8.1External Link , the Drupal community recognised the benefits of moving to native PHP attributes. These attributes offer a more robust, error-resistant way to define plugin metadata directly in the PHP code, reducing the reliance on magic strings in docblocks.

Implementing the transition in Drupal 10.3+

In Drupal 10.2, the transition began with the conversion of block and action plugins to use PHP attributes. This change allows developers to benefit from IDE features like autocompletion and type hints, making the development process smoother and less error-prone. In Drupal 10.3+, this conversion effort expands to include a broader range of plugins, with a focus on ensuring compatibility with the new system.

Lee explains that module maintainers need to create new attribute classes to replace existing annotations. These classes are typically placed in a new Attribute namespace within the module. The process involves defining a constructor that mirrors the structure of the original annotation, ensuring that all necessary metadata is captured in the new attribute format.

To facilitate the transition, developers must also update their plugin managers to recognise the new attribute classes. This involves modifying the plugin manager’s constructor to pass the new attribute name, enabling the system to differentiate between old annotations and new attributes. Drupal 10.3+ maintains backward compatibility by supporting both the old and new systems, although deprecation notices will soon warn developers to update their code.

Preparing for Drupal 11 and beyond

The Drupal community is committed to supporting both annotations and attributes throughout Drupal 11, with plans for full adoption of PHP attributes by Drupal 12. To ease the transition, a Rector rule has been developed to automate the conversion of existing code, which will be integrated into the project update bot to assist maintainers in adapting their modules.

Lee emphasises that this transition, while extensive, is a necessary step towards modernising Drupal’s architecture and aligning it with contemporary PHP practices. By adopting PHP attributes, Drupal developers can enjoy a more intuitive and reliable development experience, ultimately leading to better code quality and maintainability.

The session concludes with a call to action for developers to begin the conversion process in their own modules, taking advantage of the tools and resources available to ensure a smooth transition to the new system.