Refactor to #[Hook]
Refactor to #[Hook]
Fabian Bircher (Bircher)
Some Hooks became plugins or event subscribers in Drupal 8.
But now it is again to time to refactor! The vast majority of procedural hooks need to be transformed to object oriented hooks.
Prerequisite
A basic understanding of what a hook in Drupal is.
Target Audience
Drupal developers
Outline
Code for Drupal 11.1 looks almost like magic again for someone who was used to the magic naming that kept working the same way even in Drupal 11.0.
Hook_block_info has become a plugin a long time ago, and recently plugins changed to use php annotations.
But the big change that happened in Drupal 11.1 is that most of the hooks can now be methods on classes. This allows for better code organisation and better testing because one can use dependency injection.
We will showcase and demo that implementing hooks the new way is just as easy.
And even refactoring everything is nothing to be afraid of.
Everyone should know about #[Hook]
Learning Objectives
You will learn how to implement hooks in modern Drupal and how to refactor your old code to remain compatible with future versions of Drupal.
Experience level
Anyone