Topic

Hooks

Last updated August 24, 2017

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.

Example tasks

  • 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

Confidence

Hooks have been a staple of Drupal module development since the beginning. In Drupal 8, "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 Drupal 8.

Drupalize.Me resources

  • What Are Hooks?
    • Understand what hooks are and how they are used in Drupal. Be able to know where to find information about implementing and invoking hooks from your custom code.
  • Implement Any Hook
    • Locate the documentation for a known hook and then implement it in your custom module.
  • Discover Existing Hooks
    • Find the hook you need to implement.
  • Understand Drupal Events Versus Hooks
    • In this tutorial, you'll learn all about events versus hooks in Drupal 8, and we'll introduce the concept of event listeners.
  • The Event-Driven Hook System (Drupal 7)
    • This tutorial describes how Drupal modules are able respond to specific events through the hook system in Drupal 7.
  • Entity API Hooks
    • Learn about the various hooks we can use to interact with entities.
  • Modify Existing Entities with Alter Hooks
    • Understand how to override the default behavior of an entity type provided by Drupal core (or another contributed module) within your custom code.
  • Field API Hooks
    • Find and implement appropriate hooks to alter existing field types, field widgets, or field formatters.
  • Discover Existing Theme Hook Suggestions
    • Learn how to find the names of theme hook suggestions for any template file.
  • Add New Theme Hook Suggestions
    • Understand how to add, alter, remove, or reorder the list of theme hook suggestions for a template file.
  • Use hook_migrate_prepare_row()
    • Use hook_migrate_prepare_row() and hook_migrate_MIGRATION_ID_prepare_row() to exclude some node types and fields from being migrated during a custom Drupal-to-Drupal migration.

More Guides

We have guides on many Drupal skills and topics.

Explore guides

External resources

  • Hooks (api.drupal.org)
    • API reference page that describes how hooks operate in Drupal, with a list of hooks available for developers to implement.