Drupal Hooks

Drupal's hook system is a fundamental part of its architecture, providing a powerful way for modules to interact with and alter the core behavior of the platform. Whenever a specific event occurs, Drupal invokes a corresponding hook, allowing any module to react and modify the outcome.

What are Hooks?

In Drupal, a hook is a PHP function that follows a strict naming convention: MODULENAME_hookname(). When a module implements a hook, it essentially tells Drupal to execute its custom code at a specific point in the request lifecycle. The core module_invoke_all() function is responsible for finding and executing all implementations of a given hook.

Common Examples

  • hook_menu() (Drupal 7): Used to register menu items, page callbacks, and access control.
  • hook_node_insert(): Invoked after a new node is created and saved. Perfect for triggering actions on new content.
  • hook_node_view(): Allows modules to alter the build array of a node before it is rendered.
  • hook_form_alter(): A powerful aliased hook (hook_form_FORM_ID_alter) that lets you modify almost any form in Drupal.
  • hook_user_login(): Allows code to be executed immediately after a user successfully logs in.

Hooks in Modern Drupal (8/9/10)

With the adoption of Symfony in Drupal 8, new paradigms like Plugins and the Event Dispatcher were introduced. However, hooks have largely been modernized rather than eliminated. Many classic hooks have been renamed to follow patterns like hook_ENTITY_TYPE_insert(). The powerful hook_form_alter() remains a primary tool for customizing forms. Understanding the hook system is an essential skill for any Drupal developer.

For more detailed tutorials and practical use cases, check out the Drupal section of this site, or return to the homepage for a full overview of available projects and content.