Upgrading to class-based hooks in Drupal 11

Starting with Drupal 11, you can define hooks as class-based attributes that align with modern PHP practices to improve the ease of maintenance.

Although the traditional procedural hooks that are defined as functions in the .module files still supports backward compatibility, use class-based syntax for Drupal 11. The suggested directory for defining hook classes is src/Hook/. For more information, see Drupal 11.1.0 release blog.

Important:
  • Hooks are now annotated with the #[Hook] PHP attribute to indicate their role in the system.
  • You cannot convert all hooks to class-based definitions. Some older or low-level hooks must remain as procedural functions.
  • Before converting hooks, consult the official API documentation to ensure compatibility.

The following sections provide examples of the most common updates that you might need to make. However, you must check the deprecation lists for Drupal and PHP to ensure that you cover all the scenarios in your Developer Portal sites.

Notable changes the update brings

Some of the key changes in Drupal 11 include:

  • The Drupal 11 upgrade comes with a major Symfony upgrade version. Drupal 10 used Symfony 6, and Drupal 11 now uses Symfony 7. If you are using any Symfony, ensure that they are not deprecated. For more information, see https://symfony.com/doc/current/setup/upgrade_major.html.

  • The default Drush version is now Drush 13. If you are writing Drush commands in your custom modules, you must ensure that the commands are Drush 13 compatible.

Note:
  • The custom modules and themes must be compatible with Drupal 11. For more information about finding the custom modules and themes on your Developer Portal, see How to find your custom modules and themes on your Developer Portal.
  • It is your responsibility to check the deprecation lists for Drupal and PHP to ensure that you cover all scenarios in your Developer Portal sites.
For more information about making your custom modules compatible with Drupal 11, see the following links: