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
Comment | File | Size | Author |
---|---|---|---|
#9 | interdiff.txt | 703 bytes | Anonymous (not verified) |
#9 | base_hook_key_in-2457295-9.patch | 1.26 KB | Anonymous (not verified) |
#5 | base_hook_key_in-2457295-5.patch | 1.23 KB | Anonymous (not verified) |
#1 | base_hook_key_in-2457295-1.patch | 1.23 KB | lauriii |
Comments
Comment #1
lauriiiThat would have to be done after whole registry has been built because otherwise the registry might not be complete. Not sure if that is something we would like to do but it might not have to be too complicated.
Comment #2
lauriiiComment #3
willzyx CreditAttribution: willzyx commentednot sure.. maybe it should be
if (empty($info['render element']) && !empty($base_hook_info['render element'])) {
Comment #5
Anonymous (not verified) CreditAttribution: Anonymous at Druid commentedremoved one excess ! mark, seems to work now properly but needs tests
Comment #6
Anonymous (not verified) CreditAttribution: Anonymous at Druid commentedComment #9
Anonymous (not verified) CreditAttribution: Anonymous at Druid commentedfixed exceptions
Comment #11
lauriiiComment #13
lauriiiLeaving to NW because of tests
Comment #14
lauriiiWill be working on the tests
Comment #15
lauriiiI tested the previous solution manually and it didn't seem to be working