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

Created on 20 December 2023, 6 months ago
Updated 18 January 2024, 5 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

  • Issue created by @claudiu.cristea
  • ๐Ÿ‡ท๐Ÿ‡ดRomania claudiu.cristea Arad ๐Ÿ‡ท๐Ÿ‡ด
  • Merge request !5898Pass by reference โ†’ (Closed) created by claudiu.cristea
  • ๐Ÿ‡ท๐Ÿ‡ดRomania claudiu.cristea Arad ๐Ÿ‡ท๐Ÿ‡ด
  • ๐Ÿ‡ท๐Ÿ‡ดRomania claudiu.cristea Arad ๐Ÿ‡ท๐Ÿ‡ด

    Given it has the potential to break sites, I think it's at least major

  • Status changed to Needs review 6 months ago
  • ๐Ÿ‡ท๐Ÿ‡ดRomania claudiu.cristea Arad ๐Ÿ‡ท๐Ÿ‡ด
  • ๐Ÿ‡ท๐Ÿ‡ดRomania claudiu.cristea Arad ๐Ÿ‡ท๐Ÿ‡ด

    Unassigned

  • Issue was unassigned.
  • ๐Ÿ‡ท๐Ÿ‡ดRomania claudiu.cristea Arad ๐Ÿ‡ท๐Ÿ‡ด
  • ๐Ÿ‡ท๐Ÿ‡ดRomania claudiu.cristea Arad ๐Ÿ‡ท๐Ÿ‡ด
  • Status changed to Needs work 6 months ago
  • ๐Ÿ‡ญ๐Ÿ‡ทCroatia vulcanr Rijeka

    Just tested this locally, and it doesn't work for me unfortunatelly

  • Status changed to Needs review 6 months ago
  • ๐Ÿ‡ท๐Ÿ‡ดRomania claudiu.cristea Arad ๐Ÿ‡ท๐Ÿ‡ด

    @vulcanr, it restores the behavior before 10.2.0 for us. Could you, please, add more details about your case?

  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom catch
  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom alexpott ๐Ÿ‡ช๐Ÿ‡บ๐ŸŒ

    Given that we've regressed here, a small test of the expected functionality would be great to have.

  • Status changed to Needs work 6 months ago
  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom longwave UK

    (crosspost with @alexpott)

    Given that ThemeManager::alter() takes its second context argument ($variables) by reference does imply to me that this is intentional to some extent; if we didn't mean for that to happen we could have not done that when this code was first added. And given that the fix solves a regression and doesn't harm anything else then I think this is fine to add back in.

    Perhaps as a followup we should add test coverage for this though, if we think it should actually be a supported feature?

  • Status changed to Needs review 6 months ago
  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom longwave UK

    Added some test coverage for this (unexpected) feature.

  • Status changed to Needs work 6 months ago
  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom alexpott ๐Ÿ‡ช๐Ÿ‡บ๐ŸŒ

    I think we should update the documentation in theme.api.php to note that $variables is passed in by reference.

  • Status changed to Needs review 6 months ago
  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom longwave UK

    Also updated the docs (and fixed the first arg to hook_theme_suggestions_HOOK_alter() while I was there)

  • Status changed to RTBC 6 months ago
  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom alexpott ๐Ÿ‡ช๐Ÿ‡บ๐ŸŒ

    @longwave great work on the tests and the docs. Looks great.

  • ๐Ÿ‡จ๐Ÿ‡ฆCanada Charlie ChX Negyesi ๐ŸCanada

    that this is intentional to some extent; if we didn't mean for that to happen we could have not done that when this code was first added.

    When this code was first added in #678714 theme_hook_suggestions was a special variable set in hook_preprocess. At this point variables and suggestions were changeable together simply because they were not separate things yet. The hook was added in #1751194 like this Drupal::moduleHandler()->alter('theme_suggestions_' . $base_theme_hook, $suggestions, $variables);. At this point both the hook documentation and the comment at this place only mentions the suggestions as alterable: function hook_theme_suggestions_HOOK_alter(array &$suggestions, array $variables) { -- $variables , not &$variables. The comment said: // Allow suggestions to be altered via hook_theme_suggestions_HOOK_alter(). instead of "Allow suggestions and variables to be altered". I searched the issue in question for $variable and there was not a single mention of the necessity of them being changeable in suggestions. So everything suggests variables was not supposed to be alterable, it just so happened alter takes everything by reference.

    As for ThemeManager::alter , that was added in #2228093 as a copy of ModuleHandler::alter. There was no special consideration, it was just copied. The issue didn't discuss any of this either.

    This is just to provide a little historical background which is all I do.

    • catch โ†’ committed 6e566f5d on 10.2.x
      Issue #3409982 by claudiu.cristea, longwave, alexpott, catch, Ghost of...
    • catch โ†’ committed 5bf40faa on 11.x
      Issue #3409982 by claudiu.cristea, longwave, alexpott, catch, Ghost of...
  • Status changed to Fixed 6 months ago
  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom catch

    Committed/pushed to 11.x and cherry-picked to 10.2.x, thanks!

  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom catch
  • ๐Ÿ‡ซ๐Ÿ‡ทFrance andypost

    Please close the mr

  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.69.0 2024