diff -u b/core/lib/Drupal/Core/Theme/Registry.php b/core/lib/Drupal/Core/Theme/Registry.php --- b/core/lib/Drupal/Core/Theme/Registry.php +++ b/core/lib/Drupal/Core/Theme/Registry.php @@ -616,9 +616,7 @@ // of processors if a matching base hook is found. foreach ($grouped_functions[$first_prefix] as $candidate) { if (preg_match("/^{$prefix}_preprocess_(((?:[^_]++|_(?!_))+)__.*)/", $candidate, $matches)) { - if (isset($cache[$matches[2]])) { - $processors[$candidate] = $matches[1]; - } + $processors[$candidate] = $matches[1]; } } } @@ -640,10 +638,12 @@ $base_hook = $hook; while (!isset($cache[$base_hook]) && $pos = strrpos($base_hook, '__')) { $base_hook = substr($base_hook, 0, $pos); - $cache[$base_hook]['preprocess functions'][] = $processor; + $cache[$hook] = $cache[$base_hook]; + $cache[$hook]['base hook'] = $base_hook; + $cache[$hook]['preprocess functions'][] = $processor; // If the current hook is based on a pattern, get the base hook. - if (isset($cache[$base_hook]['base hook'])) { - $base_hook = $cache[$base_hook]['base hook']; + if (isset($cache[$hook]['base hook'])) { + $base_hook = $cache[$hook]['base hook']; } } } diff -u b/core/modules/system/src/Tests/Theme/ThemeTest.php b/core/modules/system/src/Tests/Theme/ThemeTest.php --- b/core/modules/system/src/Tests/Theme/ThemeTest.php +++ b/core/modules/system/src/Tests/Theme/ThemeTest.php @@ -303,7 +303,16 @@ drupal_theme_rebuild(); for ($i = 0; $i < 2; $i++) { $this->drupalGet('theme-test/preprocess-suggestions'); - $this->assertText('Theme hook implementor=test_theme_preprocess_theme_test_preprocess_suggestions__suggestion().', 'Theme hook ran with data available from a preprocess function for the suggested hook.'); + $items = $this->CssSelect('.suggestion'); + $expected_values = [ + 'Suggestion', + 'Kitten', + 'Kitten', + 'Flamingo', + ]; + foreach ($items as $key => $item) { + $this->assertEqual((string) $item, $expected_values[$key]); + } } } } diff -u b/core/modules/system/tests/modules/theme_test/src/ThemeTestController.php b/core/modules/system/tests/modules/theme_test/src/ThemeTestController.php --- b/core/modules/system/tests/modules/theme_test/src/ThemeTestController.php +++ b/core/modules/system/tests/modules/theme_test/src/ThemeTestController.php @@ -148,7 +148,17 @@ * suggestions. */ function preprocessSuggestions() { - return array('#theme' => 'theme_test_preprocess_suggestions__suggestion'); + return [ + [ + '#theme' => 'theme_test_preprocess_suggestions', + '#foo' => 'suggestion', + ], + [ + '#theme' => 'theme_test_preprocess_suggestions', + '#foo' => 'kitten', + ], + ['#theme' => 'theme_test_preprocess_suggestions__kitten__flamingo'], + ]; } } diff -u b/core/modules/system/tests/modules/theme_test/templates/theme-test-preprocess-suggestions.html.twig b/core/modules/system/tests/modules/theme_test/templates/theme-test-preprocess-suggestions.html.twig --- b/core/modules/system/tests/modules/theme_test/templates/theme-test-preprocess-suggestions.html.twig +++ b/core/modules/system/tests/modules/theme_test/templates/theme-test-preprocess-suggestions.html.twig @@ -1 +1,4 @@ -{{ foo }} +
{{ foo }}
+{% if bar %} +
{{ bar }}
+{% endif %} diff -u b/core/modules/system/tests/modules/theme_test/theme_test.module b/core/modules/system/tests/modules/theme_test/theme_test.module --- b/core/modules/system/tests/modules/theme_test/theme_test.module +++ b/core/modules/system/tests/modules/theme_test/theme_test.module @@ -56,7 +56,10 @@ 'function' => 'test_theme_not_existing_function', ); $items['theme_test_preprocess_suggestions'] = array( - 'variables' => array('foo' => ''), + 'variables' => [ + 'foo' => '', + 'bar' => '', + ], ); return $items; } @@ -96,14 +99,14 @@ * Implements hook_theme_suggestions_HOOK(). */ function theme_test_theme_suggestions_theme_test_preprocess_suggestions($variables) { - return array('theme_test_preprocess_suggestions__' . 'suggestion'); + return ['theme_test_preprocess_suggestions__' . $variables['foo']]; } /** * Implements hook_preprocess_HOOK(). */ function theme_test_preprocess_theme_test_preprocess_suggestions(&$variables) { - $variables['foo'] = 'Theme hook implementor=theme_theme_test_preprocess_suggestions().'; + $variables['foo'] = 'Theme hook implementor=theme_theme_test_preprocess_suggestions().'; } /** diff -u b/core/modules/system/tests/themes/test_theme/test_theme.theme b/core/modules/system/tests/themes/test_theme/test_theme.theme --- b/core/modules/system/tests/themes/test_theme/test_theme.theme +++ b/core/modules/system/tests/themes/test_theme/test_theme.theme @@ -108,6 +108,8 @@ /** * Tests a theme overriding a default hook with a suggestion. + * + * Implements hook_preprocess_HOOK(). */ function test_theme_preprocess_theme_test_preprocess_suggestions(&$variables) { $variables['foo'] = 'Theme hook implementor=test_theme_preprocess_theme_test_preprocess_suggestions().'; @@ -118,5 +120,19 @@ */ function test_theme_preprocess_theme_test_preprocess_suggestions__suggestion(&$variables) { - $variables['foo'] = 'Theme hook implementor=test_theme_preprocess_theme_test_preprocess_suggestions__suggestion().'; + $variables['foo'] = 'Suggestion'; +} + +/** + * Tests a theme overriding a default hook with a suggestion. + */ +function test_theme_preprocess_theme_test_preprocess_suggestions__kitten(&$variables) { + $variables['foo'] = 'Kitten'; +} + +/** + * Tests a theme overriding a default hook with a suggestion. + */ +function test_theme_preprocess_theme_test_preprocess_suggestions__kitten__flamingo(&$variables) { + $variables['bar'] = 'Flamingo'; } only in patch2: unchanged: --- a/core/lib/Drupal/Core/Theme/ThemeManager.php +++ b/core/lib/Drupal/Core/Theme/ThemeManager.php @@ -286,12 +286,10 @@ protected function theme($hook, $variables = array()) { 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']; } } if (isset($info['preprocess functions'])) { only in patch2: unchanged: --- /dev/null +++ b/core/modules/system/tests/themes/test_theme/templates/theme-test-preprocess-suggestions--suggestion.html.twig @@ -0,0 +1 @@ +
{{ foo }}