Drupal 11.1 Adds Hooks as Classes: A History, How-To, and Tutorials We've Updated
With the release of Drupal 11.1, there’s a cool new feature for developers: Hooks can now be implemented as class methods using PHP attributes instead of functions. This change is a major step forward in modernizing Drupal’s codebase. While procedural function-based hooks are still supported (and will be for some time), developers writing new code should strongly consider using the object-oriented (OOP) approach introduced in Drupal 11.1.
One of our core commitments at Drupalize.Me is ensuring that our tutorials remain accurate and relevant as Drupal evolves. So we’re working on updating all of our tutorials to take into account the new OOP approach to adding hooks in a module. We’re also aware that procedural hooks have been around for 24 years, and aren’t going to disappear overnight. You’ll see them in example code and existing documentation for a long time to come. So for now we’ll be including both approaches in our content whenever doing so makes sense.
You should plan on learning both approaches, and then using the one that makes the most sense given your specific case.
Continue reading to learn a bit about the evolution of hooks in Drupal core and how to implement hooks as classes in Drupal 11 in this latest Drupalize.Me blog post by Joe Shindelar.