Base theme hooks are executed in the incorrect order

Created on 24 August 2018, over 6 years ago
Updated 25 June 2024, 7 months ago

Refer to comment #29 in the issue: "Incorrect order and duplicate theme hook suggestions" 🐛 Incorrect order and duplicate theme hook suggestions Needs work

I have noticed this same issue, to reiterate the comment above, lets say I have the following theme dependencies:

- foo
-- bar
--- baz

Where baz is the currently enabled theme, which extends bar, which extends foo.

If we were to use hook_theme_suggestions_alter() as an example, Drupal is currently calling these hook implementations in the following order:

  • bar_theme_suggestions_alter()
  • foo_theme_suggestions_alter()
  • baz_theme_suggestions_alter()

This essentially means that bar extends foo, yet foo's hooks override bar.

This isn't the intended order is it?

I beleive this issue stems from \Drupal\Core\Theme\ThemeInitialization::getActiveThemeByName() where it gathers the $base_themes in ascending (reverse) order.

This list of base themes is then used in \Drupal\Core\Theme\ThemeManager::alterForTheme() which takes the list of base themes in reverse order, then tacks on the current theme.

One potential solution for this could be modify \Drupal\Core\Theme\ThemeManager::alterForTheme() to change:

    $theme_keys = [];
    foreach ($theme->getBaseThemes() as $base) {
      $theme_keys[] = $base->getName();
    }

to:

    $theme_keys = [];
    foreach ($theme->getBaseThemes() as $base) {
      $theme_keys[] = $base->getName();
    }
    $theme_keys = array_reverse($theme_keys);
🐛 Bug report
Status

Closed: outdated

Version

11.0 🔥

Component
Theme 

Last updated about 9 hours ago

Created by

🇨🇦Canada mdolnik

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024