'base hook' key in hook_theme() is limited in what it inherits from the base hook

Created on 22 March 2015, over 9 years ago
Updated 10 September 2024, about 1 month ago

Problem/Motivation

You can see from issues like #2269559: Defining theme hook suggestion in hook_theme does not work โ†’ , that 'base hook' doesn't necessarily work the way people would expect it to, and 'base hook' is used in many more places in core than in D7 and could potentially be used more to remove some special case templates etc.

Proposed resolution

Explore whether it would make sense for 'base hook' to inherit/override more than just 'includes' and 'preprocess functions' from the base hook's definition as it currently does. hook_theme() keys to inherit if not present:

  • variables / render element
  • โ€ฆothers?
\Drupal\Core\Theme\ThemeManager::theme()
if (isset($info['base hook'])) {
      $base_hook = $info['base hook'];
      $base_hook_info = $theme_registry->get($base_hook);
      // Include files required by the base hook, since its variable
      // preprocessors might reside there.
      if (!empty($base_hook_info['includes'])) {
        foreach ($base_hook_info['includes'] as $include_file) {
          include_once $this->root . '/' . $include_file;
        }
      }
      // Replace the preprocess functions with those from the base hook.
      if (isset($base_hook_info['preprocess functions'])) {
        // Set a variable for the 'theme_hook_suggestion'. This is used to
        // maintain backwards compatibility with template engines.
        $theme_hook_suggestion = $hook;
        $info['preprocess functions'] = $base_hook_info['preprocess functions'];
      }
    }

Ideally this could be done on the registry level, not inside \Drupal\Core\Theme\ThemeManager::theme(). Refactoring the code above may be outside of the scope of this issue though.

Remaining tasks

Discuss.

User interface changes

n/a

API changes

TBD

๐Ÿ“Œ Task
Status

Closed: outdated

Version

11.0 ๐Ÿ”ฅ

Component
Themeย  โ†’

Last updated about 10 hours ago

Created by

๐Ÿ‡จ๐Ÿ‡ฆCanada star-szr

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.

  • ๐Ÿ‡ณ๐Ÿ‡ฟNew Zealand quietone

    There has been no discussion here for 9 years. Before continuing can anyone confirm if this is still applicable to Drupal 10 or later?

    Since we need more information to move forward with this issue, I am setting the status to Postponed (maintainer needs more info). If we don't receive additional information to help with the issue, it may be closed after three months.

    Thanks!

  • Status changed to Closed: outdated about 1 month ago
  • ๐Ÿ‡ณ๐Ÿ‡ฟNew Zealand quietone

    Another 5 months and there is no confirmation that this still needs work. Therefor, I am closing this issue.

Production build 0.71.5 2024