By lemming on
Change record status:
Published (View all published change records)
Project:
Introduced in branch:
9.4.x
Introduced in version:
9.4.0
Issue links:
Description:
Drupal preprocess callbacks are normally defined by hook_preprocess_HOOK() in modules or themes. These functions are discovered and added to the theme registry and applied to theme elements as they are rendered.
The theme manager (@theme.manager service) only allowed these preprocess methods to be functions, while other Drupal APIs such as the Form API allows for PHP callables to be used as callbacks. This change allows PHP callables to be similarly be used as theme preprocess callbacks.
Contributed modules and themes will be able to add preprocess callbacks by implementing hook_theme_registry_alter() and adding the theme preprocess callbacks to the theme registry.
function mymodule_theme_registry_alter(&$theme_registry) {
if (!empty($theme_registry['node'])) {
// Object that implements function __invoke().
$theme_registry['node']['preprocess functions'][] = new Invokable();
// Array with class instance and method name.
$preprocess = new ThemePreprocess();
$theme_registry['node']['preprocess functions'][] = [$preprocess, 'objectMethodName'];
}
}
Impacts:
Module developers
Themers