Hooks have been Drupal's way of letting modules react to each other since the beginning: a function called mymodule_form_alter() in mymodule.module was found by its name and called. Over the Drupal 11 cycle that changed. Hooks became methods on classes, found by an attribute, and in Drupal 12 the old way is mostly gone. This guide shows the new way and what changed in 12.
How we got here
- Drupal 11.1 introduced hook classes with the
#[Hook]attribute, and#[LegacyHook]for backwards compatibility (change record 3442349). - Drupal 11.2 added ordering with
Order,OrderBeforeandOrderAfter(3493962),#[ReorderHook](3497308) and preprocess hooks in classes for modules (3496491). - Drupal 11.3 let themes use hook classes too (3551652).
What changed in Drupal 12
- Drupal 12 no longer collects procedural hooks outside the main extension file. A hook function in an
.incfile, for example, is not found any more (change record 3613767). - The
.module,.themeand.profilefiles were deprecated in Drupal 11.5. They still work in 12, but new code should not need them, and core's own modules and themes have moved their hooks into classes. Core's Default Admin theme, for example, has no.themefile at all.
A hook class
Hook classes live in the module's src/Hook folder, in the namespace Drupal\mymodule\Hook. Each method that implements a hook gets a #[Hook] attribute with the hook name, without the hook_ prefix:
<?php
declare(strict_types=1);
namespace Drupal\mymodule\Hook;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Hook\Attribute\Hook;
use Drupal\Core\Session\AccountInterface;
/**
* Form hooks for My module.
*/
final class FormHooks {
public function __construct(
private readonly AccountInterface $currentUser,
) {}
/**
* Adds a note to the node form for editors.
*/
#[Hook('form_node_form_alter')]
public function nodeFormAlter(array &$form, FormStateInterface $form_state): void {
if ($this->currentUser->hasPermission('administer nodes')) {
$form['editor_note'] = ['#markup' => 'Remember the summary.'];
}
}
}
Three things to notice:
- No service definition needed. Classes with
#[Hook]attributes are registered automatically as autowired services, so the constructor can ask for services by type. - The method name is free. Only the attribute decides which hook it implements, so name methods after what they do.
- One method, several hooks. Add several
#[Hook]attributes to the same method. And one module can implement the same hook in several classes.
A few hooks may still only be implemented once per module: hook_library_info_build(), hook_mail(), hook_help() and hook_node_update_index().
Controlling the order
Before, the order of hook implementations was set with module weights or hook_module_implements_alter(), which is no longer part of the documented hook API in Drupal 12. Now the order is an argument to the attribute:
use Drupal\Core\Hook\Order\Order;
use Drupal\Core\Hook\Order\OrderAfter;
#[Hook('entity_presave', order: Order::First)]
public function setDefaults(...) {}
#[Hook('form_alter', order: new OrderAfter(['content_moderation']))]
public function afterModeration(...) {}
Order::First and Order::Last put an implementation at the start or end. OrderBefore and OrderAfter take a list of modules, or specific class and method pairs, to be placed relative to.
Changing other modules' hooks
Two attributes let a module act on hook implementations it does not own:
#[RemoveHook]removes another implementation, given the hook name, the class and the method.#[ReorderHook]moves another implementation, with the same arguments plus an order.
And #[HookDependsOnModule] registers a hook class or method only when a given module is installed. That avoids checks inside the hook, and lets a class inject services from that module safely.
Hooks that must stay procedural
The install and update hooks still live in mymodule.install as plain functions:
hook_install(),hook_uninstall(),hook_schema()hook_update_N(),hook_post_update_NAME(),hook_removed_post_updates()hook_update_dependencies(),hook_update_last_removed()hook_install_tasks()andhook_install_tasks_alter()(for profiles) – both deprecated since 11.5 in favour of a class implementing\Drupal\Core\Extension\Install\TasksInterface
Supporting Drupal 10 and 12 in the same module
A contributed module that still supports old Drupal versions can keep both: the hook class for new versions, and a procedural function marked with #[LegacyHook] that calls the class for old ones. Versions that know about hook classes skip the legacy function, so the hook does not run twice. The #[ExtensionFileIsConverted] attribute, placed on the first function in a .module or .theme file, silences the deprecation notice for such files – core's own documentation says it is only for that kind of legacy support.
Converting an old module
- Create
src/Hook/and one class per area (forms, entities, theme). - Move each
mymodule_HOOK()function into a method with#[Hook('HOOK')]. Replace\Drupal::service()calls with constructor injection while you are at it. - Move code from
.incfiles the same way – in Drupal 12 hooks there are not found at all. - Replace
hook_module_implements_alter()withorder:,#[ReorderHook]or#[RemoveHook]. - When the
.modulefile is empty, delete it.
For the rest of what custom code needs for Drupal 12, see Getting custom code ready for Drupal 12.