Event subscribers: A new hook system. ======================================= :Published at: March 2, 2020 :Author: Erlend ter Maat :Tags: Drupal, Drupal hook system ---- Last time I wrote `about Drupal's services `_ mechnanism. Today I show what this technology has to offer for working OO in letting modules talk with each other: the hook system. I was a little disappointed when I first saw Drupal 8. So many things were improved, but why did I still see this method-name-guessing mechanism of hooks? I did not follow. Until today. I guess this legacy code keeps following us a while. There is good news on this subject too. You are not forced to stick to this legacy method. I'm pleased to introduce to you it's younger brother! hook_menu_alter... (A Confession) --------------------------------- I was disappointed. That's true. Actually I was disappointed because 'they' removed hook_menu_alter. Without asking me first! How could this happen? It happened to be one of the first hooks the Drupal code developers phased out. From now on the things you did at hook_menu_alter are solved in this fashion:: # custom_module.services.yml services: custom_module.routing class: Drupal\custom_module\Routing\CustomRouteSubscriber tags: - { name: 'event_subscriber' } .. code-block:: php `_ at the 'tabledrag_example' module. Conclusion ---------- Event subscriber services do solve an important 'problem' of the former hook system in terms of OO and (therefore) readability, maintainability. For further reading I refer to the drupal documentation, where hou can find an example on how you can create your own hooks in this fashion is found on drupal.org in the article "`Subscribe to and dispatch events `_". Next time... ------------ The most common way to access services is by means of `depencency injection `_. Next blog post I will show how powerfull that is, and how this power can be used properly.