At the end of last year Drupal introduced object oriented hooks to replace the procedural ones. This introduced the phasing out of the module file.
Version 11.2 introduces the preprocess hooks that where still procedural until this version.
So now the only file with procedural hooks is the install file.
What changed?
The hook_preprocess and hook_preprocess_HOOK hooks are now object oriented. These are the two hooks that are frequently used in the theme file. Other frequently used hooks already had an object oriented version.
In addition to adding the new hooks they also made the execution order of the procedural and object oriented hooks saner.
There are several order changes:
- The
Hook
attribute now has an order argument. - A ReOrderHook attribute is added to change the order of the hook in other modules. This feels a bit dangerous, but I can think of cases where it is needed.
Another new attribute that changes the behaviour of a hook in other modules is RemoveHook. If ReOrderHook
is a penknife, RemoveHook
is a bowie knife.
On the front of making the developer experience for the object oriented hooks better, they added the PREFIX
and SUFFIX
constants to the Hook class.
I'm shamelessly going to steal the examples.
// before
#[Hook('form_alter')]
#[Hook('hook_form_FORM_ID_alter')]
// after
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
class FormAlter extends Hook {
/**
* {@inheritdoc}
*/
public const string PREFIX = 'form';
/**
* {@inheritdoc}
*/
public const string SUFFIX = 'alter';
public function __construct(
string $form_id = '',
public string $method = '',
public ?string $module = NULL,
) {
parent::__construct($form_id, $method, $module);
}
}
#[FormAlter]
#[FormAlter('FORM_ID')]
update: the FormAlter attribute is added to version 11.2.
Conclusion
Drupal 11.2 feels like a big release with all the hook changes.
The version is expected to be released in the middle of June.
I can't wait to update.