Hooks allow modules to alter and extend the behavior of Drupal core, or another module. They are one of several ways that code components in Drupal can communicate with one another. Using hooks, a module developer can change how core or another module works—without changing the existing code. As a Drupal developer, understanding how to implement and invoke hooks is essential.
- Get information, such as a list of links that should be placed in a user toolbar
- Alter something, such as adding or removing a field from the content edit form
- React to a system action, such as when a user cancels their account
Hooks have been a staple of Drupal module development since the beginning. In Drupal, “info” hooks have generally been replaced by the plugin system and many “reactive” hooks have been replaced by events. Check api.drupal.org to see if a hook exists for the latest version of Drupal.
Below are some examples of the types of things you can do with hooks.
- API reference page that describes how hooks operate in Drupal, with a list of hooks available for developers to implement.
Change record: All hook invocation delegated to Module Handler service (Drupal.org)
- Introduced in 9.4.x, all hook invocations have been been delegated as a responsibility of Module Handler service. Modules must no longer construct hook functions on their own. This will allow future improvement to the hook system since hooks/alters are fully centralized.