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

Files: 
CommentFileSizeAuthor
#9 interdiff.txt703 bytesb0unty
#9 base_hook_key_in-2457295-9.patch1.26 KBb0unty
PASSED: [[SimpleTest]]: [PHP 5.4 MySQL] 95,706 pass(es). View
#5 base_hook_key_in-2457295-5.patch1.23 KBb0unty
FAILED: [[SimpleTest]]: [PHP 5.4 MySQL] 90,449 pass(es), 0 fail(s), and 8 exception(s). View
#1 base_hook_key_in-2457295-1.patch1.23 KBlauriii
FAILED: [[SimpleTest]]: [PHP 5.4 MySQL] 90,116 pass(es), 0 fail(s), and 12 exception(s). View

Comments

lauriii’s picture

Status: Active » Needs review
FileSize
1.23 KB
FAILED: [[SimpleTest]]: [PHP 5.4 MySQL] 90,116 pass(es), 0 fail(s), and 12 exception(s). View

That 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.

lauriii’s picture

Issue tags: +Needs tests
willzyx’s picture

+++ b/core/lib/Drupal/Core/Theme/ThemeManager.php
@@ -286,6 +286,20 @@ protected function theme($hook, $variables = array()) {
+      // If render element haven't been defined for current element try to get
+      // it from the base hook.
+      if (!empty($info['render element']) && !empty($base_hook_info['render element'])) {

not sure.. maybe it should be if (empty($info['render element']) && !empty($base_hook_info['render element'])) {

Status: Needs review » Needs work

The last submitted patch, 1: base_hook_key_in-2457295-1.patch, failed testing.

b0unty’s picture

FileSize
1.23 KB
FAILED: [[SimpleTest]]: [PHP 5.4 MySQL] 90,449 pass(es), 0 fail(s), and 8 exception(s). View

removed one excess ! mark, seems to work now properly but needs tests

b0unty’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 5: base_hook_key_in-2457295-5.patch, failed testing.

b0unty’s picture

Status: Needs work » Needs review
FileSize
1.26 KB
PASSED: [[SimpleTest]]: [PHP 5.4 MySQL] 95,706 pass(es). View
703 bytes

fixed exceptions

lauriii’s picture

Status: Needs review » Needs work
Issue tags: +Needs change record

lauriii’s picture

Issue tags: -Needs change record

Leaving to NW because of tests

lauriii’s picture

Assigned: Unassigned » lauriii

Will be working on the tests

lauriii’s picture

Assigned: lauriii » Unassigned

I tested the previous solution manually and it didn't seem to be working

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.