Drupal 11.2 Adds Hook Implementation Ordering with New Order Parameter
Drupal 11.2 brings a major advancement for module and distribution developers with the introduction of hook implementation ordering through a new `order`
parameter. This feature allows developers to precisely control the execution sequence of hook implementations using the `#[Hook]`
attribute.
The initial implementation was written by Karoly Negyesi, followed by a significantly improved version by Andreas Hennings. Testing and extensive collaboration were led by Nic Laflin, who managed over 200 issue comments and numerous merge request discussions. Their combined effort spanned 23 issue branches, marking a major collaborative milestone for the Drupal community.
The `order`
parameter supports simple options like `Order::First`
and `Order::Last`
, as well as more complex structures such as `OrderBefore`
and `OrderAfter
`. These advanced options allow developers to specify ordering relative to other modules or to specific classes and methods.
Examples include:
-`#[Hook('some_hook', order: Order::First)]`
to prioritize execution.
- `#[Hook('some_hook', order: new OrderBefore(['other_module']))]`
to run before another module’s hook.
- Using class and method references for even finer control with `OrderBefore`
and `OrderAfter`
.
Developers who previously relied on `hook_module_implements_alter`
must now use the new functionality, supported by the `#[LegacyModuleImplementsAlter]`
attribute for backwards compatibility.
For complete details and technical examples, visit the official Drupal change record.