Chapter 6: Hooks and Events

This chapter is part of the Drupal Module Developer Guide.

Hooks and events are 2 ways that modules can alter existing Drupal features, and respond to what other code in Drupal is doing. They are a key part of what makes it possible for custom modules to modify almost any aspect of how Drupal works. In this chapter we’ll learn how to discover, and implement, both of these fundamental APIs.

Tutorials in this course
Categories
Drupal 8, 9, and 10
More information

Hooks in Drupal enable modules to alter or extend the behavior of Drupal core or other modules. By implementing functions with specific names, modules can intervene at various points in Drupal's execution flow. This tutorial introduces hooks, their implementation, and their significance in module interaction.

In this tutorial, we'll:

  • Define what hooks are.
  • Explore how hooks are implemented in modules.
  • Understand when modules should define and invoke new hooks.

By the end of this tutorial, you'll understand the concept of hooks as a means to alter Drupal's behavior.

Categories
Drupal 8, 9, and 10
More information

The event system in Drupal enables different components to interact and communicate. Through events, a component can announce important actions to interested modules, providing a flexible way to extend functionality. This system is central to Drupal's event-driven architecture.

In this tutorial, we'll:

  • Define events and their operation.
  • Provide examples of dispatching and subscribing to events.
  • Contrast events with hooks.

By the end of this tutorial you should be able to understand the event system's fundamentals in Drupal, and how modules can use it for extending and modifying functionality.

More information

To use a hook in a module, you'll need to know how to find the API documentation for the hook. The API documentation for a hook describes the hook's purpose, provides the function signature, and defines each of its parameters.

In this tutorial, we'll:

  • Explore methods for listing available hooks.
  • Guide you to the documentation for specific hooks.

By the end of this tutorial, you'll be able at find possible hooks for your task, and understand the documentation for a hook.

Categories
Drupal 8, 9, and 10
More information

To use a hook in a Drupal module, we need to add a function with a specific naming convention to your module's MODULE_NAME.module file. Each hook has unique arguments and an expected return value. In this tutorial, we'll walk through the process of implementing a hook by adding end-user help text for the anytown module, which Drupal's administrative UI will display. The process we'll use here applies to any hook implementation.

In this tutorial, we'll:

  • Locate the documentation for hook_help().
  • Implement the hook in the anytown module.
  • Verify our hook implementation.

By the end, you'll have implemented hook_help() to display help text in the Drupal UI.

This course appears in the following guides:
Module Development
Learn Drupal module development in this hands-on guide.

Drupal Module Developer Guide