Hooks | Drupal 8

asd

 

Hooks allow modules to alter and extend the behavior of Drupal Core or another module without modifying the existing code.

What are Hooks?

  • Define what Hooks are and how they work?

  • Understand the use-case for Hooks.

  • Examine the different types of Hooks that exist.

Hooks are specially-named functions that a module defines (this is known as "implementing the hook"), which are discovered and called at specific times to alter or add to the base behavior or data (this is known as "invoking the hook"). Each hook has a name (example: hook_batch_alter()), a defined set of parameters, and a defined return value. Your modules can implement hooks that are defined by Drupal core or other modules that they interact with. Your modules can also define their own hooks, in order to let other modules interact with them.

How Hooks work?

A naming convention for PHP functions in modules:

hook_entity_view(&$build, $entity, $display, $view_mode)

modules/mymodule/mymodule.module

Function mymodule_entity_view(&$build, $entity, $display, $view_mode)    {

//    Custom goes here…   

}

Every hook has three parts:

  • Name: For example: hook_entity_load().

  • Implementation: The implementation consists of the custom code in your module that you want to execute. Any number of modules can implement the same hook. This allows multiple modules to contribute to the final outcome. Each implementation (function) will be called in order based on the module's weight column in the system database table.

  • Definition: Another module or subsystem provides the definition, which specifies the hook's name and which arguments are passed to the implementation, as well as when the implementation will be called.

A naming convention

At their most basic, hooks are a naming convention. Just replace the word HOOK in the hook definition with the machine name of your module, and thus you've implemented a hook.

Thus HOOK_entity_load() becomes mymodule_entity_load().

Types of hooks:

Generally you can place hooks into one of three categories:

  • Hooks that answer a question

  • Hooks that alter existing data

  • Hooks that react to an action

Hooks that answer questions: Often referred to as "info hooks", are invoked when some component in Drupal is gathering a list of information about a particular topic. For example, a list of all the items that should be displayed in the toolbar, or a of requirements to verify prior to installation. These hooks primarily return arrays whose structure and values are determined by the hook definition. The user module is a good example of this: see user_toolbar() which adds links to common user account pages to the Toolbar.

Hooks that alter existing data: Often referred to as "alter hooks", and identifiable by the fact that their names are suffixed with _alter, are invoked in order to allow modules to alter a list of previously gathered information. These are often paired with info hooks. A component may first invoke an info hook to gather a list of information, then immediately invoke an alter hook to allow anyone to alter the list that was just created before it's used. You might, for example, change the order that items are listed in the Toolbar, or even change the name used for an item added by another module. Of special note is hook_form_alter(), one of the most powerful, and likely most commonly implemented hooks. hook_form_alter() allows modules to alter the Form API arrays generated by other modules, thereby modifying, and participating in, every aspect of the form generation, validation, and submission workflow.

Hooks that react to an action: Similar to events, these hooks are invoked when specific actions are taken in code somewhere in the system in order to allow other code to do something based on that action. For example, hook_user_cancel() is invoked every time a user account is canceled. Modules that store data about users that need to perform cleanup tasks can implement this hook to be notified about the account that is being canceled and take the necessary actions on their own data. The comment module is a good example of this, see comment_user_cancel() which removes comments created by a user when their account is canceled.

Additional Resources:

Hooks API documentation (api.drupal.org)