diff --git a/core/includes/theme.inc b/core/includes/theme.inc index 7bff76e..5676d32 100644 --- a/core/includes/theme.inc +++ b/core/includes/theme.inc @@ -1035,6 +1035,13 @@ function theme($hook, $variables = array()) { $variables += array( 'theme_hook_original' => $original_hook, ); + // If theme() is called with a suggestion like comment__node_article, include + // information about the base hook - e.g. 'comment'. + if ($hook != $original_hook) { + $variables += array( + 'theme_hook_base' => $hook, + ); + } // Invoke the variable processors, if any. The processors may specify // alternate suggestions for which hook's template/function to use. If the diff --git a/core/modules/system/lib/Drupal/system/Tests/Theme/TwigDebugMarkupTest.php b/core/modules/system/lib/Drupal/system/Tests/Theme/TwigDebugMarkupTest.php index 38e183b..c13fdcf 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Theme/TwigDebugMarkupTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Theme/TwigDebugMarkupTest.php @@ -19,7 +19,7 @@ class TwigDebugMarkupTest extends WebTestBase { * * @var array */ - public static $modules = array('theme_test'); + public static $modules = array('theme_test', 'comment'); public static function getInfo() { return array( @@ -67,6 +67,25 @@ function testTwigDebugMarkup() { $this->assertTrue(strpos($output, '* node--2' . $extension) !== FALSE, 'Node ID specific template suggestion found.'); $this->assertTrue(strpos($output, 'x node' . $extension) !== FALSE, 'Base template file shown as current template.'); + // Create another node, call a theme suggestion directly, and make sure the + // template suggestions shown in the debug markup are correct. + $node3 = $this->drupalCreateNode(); + $comment = entity_create('comment', array( + 'cid' => NULL, + 'nid' => $node3->id(), + 'node_type' => $node3->type, + 'pid' => 0, + 'uid' => 0, + 'status' => COMMENT_PUBLISHED, + 'subject' => $this->randomName(), + 'hostname' => '127.0.0.1', + 'langcode' => 'und', + 'comment_body' => array('und' => array($this->randomName())), + )); + $comment->save(); + $output = theme('comment__node__article', comment_view($comment)); + $this->assertTrue(strpos($output, 'x comment' . $extension) !== FALSE, 'Suggested template file shown as current template.'); + // Disable debug, rebuild the service container, and clear all caches. $this->settingsSet('twig_debug', FALSE); $this->rebuildContainer(); diff --git a/core/themes/engines/twig/twig.engine b/core/themes/engines/twig/twig.engine index b541cdc..8174486 100644 --- a/core/themes/engines/twig/twig.engine +++ b/core/themes/engines/twig/twig.engine @@ -62,6 +62,9 @@ function twig_render_template($template_file, $variables) { $current_template = basename($template_file); $suggestions = $variables['theme_hook_suggestions']; $suggestions[] = $variables['theme_hook_original']; + if (isset($variables['theme_hook_base'])) { + $suggestions[] = $variables['theme_hook_base']; + } foreach ($suggestions as $key => &$suggestion) { $template = strtr($suggestion, '_', '-') . $extension; $prefix = ($template == $current_template) ? 'x' : '*';