.../Drupal/Core/Template/ThemeRegistryNodeVisitor.php | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/core/lib/Drupal/Core/Template/ThemeRegistryNodeVisitor.php b/core/lib/Drupal/Core/Template/ThemeRegistryNodeVisitor.php index 17c0aa9..80b4e60 100644 --- a/core/lib/Drupal/Core/Template/ThemeRegistryNodeVisitor.php +++ b/core/lib/Drupal/Core/Template/ThemeRegistryNodeVisitor.php @@ -22,6 +22,12 @@ * - {% extends "path/to/block--search-form-block.html.twig" %} * - {% include "path/to/block--search-form-block.html.twig" %} * + * Ensures that a variant of a theme hook that extends the non-variant does not + * skip the current theme's template: if a theme has + * block--search-form-block.html.twig that's extending block.html.twig, then it + * will use the current theme's block.html.twig if it has it, and not the parent + * theme's. This matches the behavior outside of Twig. + * * @see https://www.drupal.org/node/2387069 * * @see twig_render @@ -52,11 +58,13 @@ protected function doEnterNode(\Twig_Node $node, \Twig_Environment $env) { if ($node instanceof \Twig_Node_Module && $node->hasNode('parent')) { $parent = $node->getNode('parent'); if ($parent && $this->expressionQualifies($parent)) { - $current_file = basename($node->getAttribute('filename')); - $extended_file = $parent->getAttribute('value'); - $candidates = ($current_file === $extended_file) - ? $this->getCandidateParentTemplates($current_file) - : $this->getCandidateTemplates($extended_file); + $current_filename = basename($node->getAttribute('filename')); + $extended_filename = $parent->getAttribute('value'); + assert('strpos($current_filename, "/"") === FALSE'); + assert('strpos($extended_filename, "/") === FALSE'); + $candidates = ($current_filename === $extended_filename) + ? $this->getCandidateParentTemplates($current_filename) + : $this->getCandidateTemplates($extended_filename); if ($candidates === FALSE) { throw new \Twig_Error(sprintf('Template "%s" extends "%s", but no such parent templates exist.', $node->getAttribute('filename'), $parent->getAttribute('value'))); }