Theming
Topic

Hooks for Drupal 7, 8, 9, and 10

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, “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 the latest version of Drupal.

Drupalize.Me resources

Categories
Drupal 8, 9, and 10
More information

Hooks allow modules to alter and extend the behavior of Drupal core, or another module. They are one of the various 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.

In this tutorial we'll:

  • Define what hooks are and the types of hooks that exist
  • Understand the use case for hooks
Categories
Drupal 8, 9, and 10
More information

Hooks allow module developers to execute custom code at key moments during Drupal's request processing life cycle. They can be used to react to actions or conditions, alter forms and existing configuration, and extend Drupal in various ways. Knowing how to implement a hook is an essential skill for any Drupal developer.

In this tutorial we'll walk through the process of locating the documentation for a specific hook, and then implementing it. All hooks are implemented in the same way, so once you know how to implement one, you'll be able to implement any.

Categories
Drupal 8, 9, and 10
More information

How do you figure out what hooks are available to implement? How do you know which hook to implement to accomplish a task?

The list of hooks that are available to implement in your custom Drupal module varies depending on the modules enabled for a site. Each module can optionally invoke new hooks. There are also some hooks invoked by Drupal core subsystems like Form API that are always present. This can make it a little bit tricky sometimes to figure out what hooks are available, and which one to implement.

In this tutorial we'll look at:

  • Different ways to get a list of available hooks
  • Where to find the documentation for a hook so you can know if it's the one you want to implement

By the end of this tutorial you should be able to browse a list of hooks and their related documentation.

Categories
Drupal 8, 9, and 10
More information

As a module developer you should define and invoke new hooks in your module in order to allow other developers -- or even future you -- to extend your module's functionality without hacking your module's code. This requires:

  • Creating a new, unique, name for your hook
  • Providing documentation for your hook
  • Invoking the hook at critical points in your code

By the end of this tutorial you should have a better idea of when to define a new hook and know how to invoke a hook from your code.

More information

In this tutorial, you'll learn all about events versus hooks in Drupal 8 and we'll introduce the concept of event listeners.

Additional resources

Events — api.drupal.org

More information

This chapter describes how Drupal modules are able respond to specific events through the hook system. A couple of example hooks are implemented in order to see how this process works. This video builds on the demo module we created in the previous chapter.

Below are some examples of the types of things you can do with hooks.

Categories
Drupal 8, 9, and 10
More information

Drupal's Entity system provides several hooks that allow custom code to interact with various parts of the entity life cycle.

In this tutorial we'll:

  • Examine the available hooks
  • Learn how to make use of them to act on several different types of operations on individual entities

By the end if this tutorial you should have a better understanding of the hooks available to developers who want to respond to entity lifecycle operations and how to use them to customize the way specific entity types work.

More information

On occasion you may need to modify the behavior of entity types defined by another module. Thankfully Drupal includes several alter hooks that can be used to override the behavior of another entity.

In this tutorial we will:

  • Walk through the common Entity API hooks
  • Look at example implementations of each
  • And discuss the use cases for each

By the end of this tutorial you will have a better understanding of how to override the default behavior of an entity type provided by Drupal core (or another contributed module) within your custom code.

Categories
Drupal 8, 9, and 10
More information

Drupal's Field API specifies the implementation details for field types, widgets and formatters. It also provides several hooks that allow custom code to alter these implementation details. In this tutorial we'll take a look at these Field API hooks and see how they can be used to change field types, widgets and formatters.

By the end of this lesson, you should be able to:

  • Idenfity existing Field API hooks for manipulating field behavior
  • Understand the proper method for changing the behavior of a field type, widget or formatter
  • Know where to find the documentation for these API functions, and how to find their implementations
Categories
Drupal 8, 9, and 10
More information

When determining which template file to use to theme an element, Drupal uses the list of theme hook suggestions to look for the best match. This allows for fine-grained control over how things appear based on dynamic state and contextual information in your application. The list of theme hook suggestions varies for each base template, so we need a way to figure out our options.

In this tutorial we'll look at:

  • How to determine the list of valid theme hook suggestions for any template file
  • How theme hook suggestions are added by modules and themes

By the end of this tutorial you should be able to explain how theme hook suggestions are added, and determine the valid suggestions for any template file.

More information

Themes and modules can alter the list of theme hook suggestions in order to add new ones, remove existing ones, or reorder the list. This powerful feature allows for the definition of custom logic in your application that can tell Drupal to use different templates based on your own unique needs. You might for example; use a different page template for authenticated users, or a custom block template for someone's birthday.

In this tutorial we'll cover:

  • Adding new theme hook suggestions from a theme using hook_theme_suggestions_HOOK_alter()
  • Altering the list of theme hook suggestions
  • Removing theme hook suggestions
  • Reordering the list of theme hook suggestions
Categories
Drupal 8, 9, and 10
More information

Every row returned, from every source, during the execution of a migration is passed through hook_migrate_prepare_row() and hook_migrate_MIGRATION_ID_prepare_row(). Using these two hooks we can augment our migration in a variety of different ways.

In this tutorial we'll:

  • Discuss the use case for hook_migrate_prepare_row()
  • Implement hook_migrate_prepare_row() and use it to skip all but a select list of fields during the field migration
  • Implement hook_migrate_MIGRATION_ID_prepare_row() and use it to skip all but a select list of node types

By the end of this tutorial you should have a better understanding of when hook_migrate_prepare_row() might be useful when writing your own migration, as well as how to skip rows in a migration based on conditional logic.

Guides

Not sure where to start? Our guides provide useful learning tracks for all skill levels.

Navigate 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.
  • Change record: All hook invocation delegated to Module Handler service (Drupal.org)
    • Introduced in 9.4.x, all hook invocations have been been delegated as a responsibility of Module Handler service. Modules must no longer construct hook functions on their own. This will allow future improvement to the hook system since hooks/alters are fully centralized.