Skip to content

Drupal Hooks

I need to... Guide Summary
Choose an implementation style: hook class, procedural function, or both Hook Implementation Decision Choosing between a #[Hook] class method, a procedural function, or both when adding or converting a hook implementation; the lowest core version supported decides it.
Read the #[Hook] attribute reference Hook Attribute Anatomy Writing or reading a #[Hook] attribute: the exact constructor, the three placement forms, discovery rules, and the two exceptions the collector enforces at container build.
Order hook implementations, or reorder/remove another module's Hook Ordering Ordering hook execution with order: on #[Hook], #[ReorderHook], or #[RemoveHook] since 11.2.0, replacing hook_module_implements_alter().
Know which hooks must stay procedural Procedural-Only Hooks Which hooks (install, update, schema, meta hooks) must stay procedural when converting a module, and what replaces hook_requirements().
Write OOP hooks in a theme Theme Hooks (OOP) Writing theme hooks as #[Hook] classes since core 11.3.0; the .theme file is deprecated in 11.5.0 (unreleased at verification).
Stop the procedural hook scan for performance Procedural Scan Performance Stopping the procedural hook scan at container build once a module has converted to #[Hook] classes, via skip_procedural_hook_scan or #[ProceduralHookScanStop].
Convert a module or theme off .module/.theme Migrating to OOP Hooks Converting a module or theme's procedural hooks to #[Hook] classes before Drupal 12.0.0 stops collecting legacy hooks outside the main extension file.
Look up a deprecation or landing version Hooks Deprecation Timeline Reference table of every hook-system mechanism's landed, deprecated, and removed versions with its change record, verified against core 11.4.5.
Apply security, performance, and coding standards to hook classes Hooks Best Practices Standards for writing or reviewing a hook class: constructor injection, class shape, naming, security in alter hooks, and production failure modes.