From 28e77873af5ddf785a0811dd01dcb8c9cf09c0df Mon Sep 17 00:00:00 2001 From: markcarver Date: Thu, 30 Jun 2016 21:42:53 +0200 Subject: [PATCH] Issue #2752443 by sylus, markcarver: In correct order and duplicate theme hook suggestions --- core/lib/Drupal/Core/Theme/ThemeManager.php | 6 ++++++ core/themes/engines/twig/twig.engine | 15 +++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/core/lib/Drupal/Core/Theme/ThemeManager.php b/core/lib/Drupal/Core/Theme/ThemeManager.php index ed18039..e6283d3 100644 --- a/core/lib/Drupal/Core/Theme/ThemeManager.php +++ b/core/lib/Drupal/Core/Theme/ThemeManager.php @@ -244,6 +244,12 @@ public function render($hook, array $variables) { $this->moduleHandler->alter($hooks, $suggestions, $variables, $base_theme_hook); $this->alter($hooks, $suggestions, $variables, $base_theme_hook); + // Append the original theme hook if it is a theme suggestion and not + // already in the suggestions array. + if (strpos($original_hook, '__') !== FALSE && !in_array($original_hook, $suggestions)) { + $suggestions[] = $original_hook; + } + // Check if each suggestion exists in the theme registry, and if so, // use it instead of the base hook. For example, a function may use // '#theme' => 'node', but a module can add 'node__article' as a suggestion diff --git a/core/themes/engines/twig/twig.engine b/core/themes/engines/twig/twig.engine index c8f8f49..e084c29 100644 --- a/core/themes/engines/twig/twig.engine +++ b/core/themes/engines/twig/twig.engine @@ -92,8 +92,19 @@ function twig_render_template($template_file, array $variables) { // Get the value of the base hook (last derived suggestion) and append it // to the end of all theme suggestions. $base_hook = array_pop($derived_suggestions); - $variables['theme_hook_suggestions'] = array_merge($derived_suggestions, $variables['theme_hook_suggestions']); - $variables['theme_hook_suggestions'][] = $base_hook; + + // Iterate over each derived suggestion and only append + // it if it's not already in the array. + foreach ($derived_suggestions as $suggestion) { + if (!in_array($suggestion, $variables['theme_hook_suggestions'])) { + $variables['theme_hook_suggestions'][] = $suggestion; + } + } + + // Add the base theme hook, if not already in the array. + if (!in_array($base_hook, $variables['theme_hook_suggestions'])) { + $variables['theme_hook_suggestions'][] = $base_hook; + } } if (!empty($variables['theme_hook_suggestions'])) { $extension = twig_extension(); -- 2.9.0