[D10.2 regression] Theme suggestions cannot alter variables anymore

Created on 20 December 2023, about 1 year ago
Updated 18 January 2024, 11 months ago

Problem/Motivation

Initially reported in #2953921-37: Refactor out theme hook suggestion building from ThemeManager::render() into a separate function. โ†’

In ๐Ÿ“Œ Refactor out theme hook suggestion building from ThemeManager::render() into a separate function. Fixed , theme suggestions building has been refactored in a protected method. But this has introduced a regression, starting with Drupal 10.2, for hook_theme_suggestions_alter (& Co.) implementations that passed the $variables array by reference. Although documentation doesn't state that $variables can be passed by reference, this is possible as, with hooks, there's no strong enforcement on signature (like with interfaces/classes).

In order to offer a variable to all suggestions, this was possible before:

function mytheme_suggestions_HOOK_alter(array &$suggestions, array &$variables): void {
  ...
  $variables['foo'] = $bar;
}

As, ThemeManager::buildThemeHookSuggestions() doesn't pass $variables by reference, the changes to the array are lost with Drupal 10.2.

Some may say that this implementation didn't follow the documentation. True, but, in the same time, it wasn't forbidden in any way. So, yes, it's a regression for sites that did implement in that way, in order to offer variable alters to all downstream suggestions.

Steps to reproduce

See the example above.

Proposed resolution

Change the signature of ThemeManager::buildThemeHookSuggestions() by passing the $variables by reference.

Remaining tasks

None.

User interface changes

None.

API changes

The $variables parameter of ThemeManager::buildThemeHookSuggestions() is passed by reference.

Data model changes

None.

Release notes snippet

๐Ÿ› Bug report
Status

Fixed

Version

10.2 โœจ

Component
Themeย  โ†’

Last updated 1 minute ago

Created by

๐Ÿ‡ท๐Ÿ‡ดRomania claudiu.cristea Arad ๐Ÿ‡ท๐Ÿ‡ด

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024