Hooks

Modifying other modules.

Drupal development preaches "Don't hack core". This is a principal that your application logic should be done in your own code, which extends Drupal core and installed modules. However, when you need to change the way those systems work, hooks allow us to apply our own custom code.

Writing Hooks

Hooks are written in a .module, .profile or .theme files. They are defined by the module that allows the change, so you can't use on a hook that module that doesn't offer one.

Hooks are written with a specific name, for example hook_form_alter() is implemented in your module as MODULE_form_alter().

Tip: Use an IDE plugin to get hook suggestions.

Tip: Find module hooks in *.api.php.

Event Subscribers

Event subscribers are classes with methods that respond to events triggered by Drupal and modules. They are similar to hooks, but are implemented differently.

There is some movement to replace hooks with event subscribers.

Use the Hook Event Dispatcher module to work with Event Subscribers instead of hooks.

Level