diff --git a/core/includes/theme.inc b/core/includes/theme.inc index e8cff8b..08ac747 100644 --- a/core/includes/theme.inc +++ b/core/includes/theme.inc @@ -1204,8 +1204,9 @@ function _template_preprocess_default_variables() { 'logged_in' => FALSE, ); - // Give modules a chance to alter the default template variables. + // Give modules and themes a chance to alter the default template variables. \Drupal::moduleHandler()->alter('template_preprocess_default_variables', $variables); + \Drupal::theme()->alter('template_preprocess_default_variables', $variables); // Tell all templates where they are located. $variables['directory'] = \Drupal::theme()->getActiveTheme()->getPath(); diff --git a/core/modules/system/src/Tests/Theme/ThemeTest.php b/core/modules/system/src/Tests/Theme/ThemeTest.php index 28d47386..ead7e56 100644 --- a/core/modules/system/src/Tests/Theme/ThemeTest.php +++ b/core/modules/system/src/Tests/Theme/ThemeTest.php @@ -322,4 +322,17 @@ public function testSuggestionPreprocessForDefaults() { } } + /** + * Check that a theme can provide default template variables. + */ + public function testDefaultTemplateVariables() { + $this->assertThemeOutput('theme_test_default_variables', [], 'Default drink: "water"' . "\n"); + $this->assertNotEqual(\Drupal::theme()->getActiveTheme()->getName(), 'test_theme'); + // Set the active theme and rebuild the regsitry. + \Drupal::theme()->setActiveTheme(\Drupal::service('theme.initialization')->initTheme('test_theme')); + \Drupal::service('theme.registry')->reset(); + $this->assertEqual(\Drupal::theme()->getActiveTheme()->getName(), 'test_theme'); + $this->assertThemeOutput('theme_test_default_variables', [], 'Default drink: "coffee"' . "\n"); + } + } diff --git a/core/modules/system/tests/modules/theme_test/templates/theme-test-default-variables.html.twig b/core/modules/system/tests/modules/theme_test/templates/theme-test-default-variables.html.twig new file mode 100644 index 0000000..864eaea --- /dev/null +++ b/core/modules/system/tests/modules/theme_test/templates/theme-test-default-variables.html.twig @@ -0,0 +1 @@ +Default drink: "{{ default_drink }}" diff --git a/core/modules/system/tests/modules/theme_test/theme_test.module b/core/modules/system/tests/modules/theme_test/theme_test.module index 8a8841e..3ca51f5 100644 --- a/core/modules/system/tests/modules/theme_test/theme_test.module +++ b/core/modules/system/tests/modules/theme_test/theme_test.module @@ -61,6 +61,9 @@ function theme_test_theme($existing, $type, $theme, $path) { 'bar' => '', ], ]; + $items['theme_test_default_variables'] = [ + 'variables' => [], + ]; return $items; } @@ -207,3 +210,10 @@ function theme_test_theme_suggestions_node(array $variables) { return $suggestions; } + +/** + * Implements hook_template_preprocess_default_variables_alter(). + */ +function theme_test_template_preprocess_default_variables_alter(array &$variables) { + $variables['default_drink'] = 'water'; +} diff --git a/core/modules/system/tests/themes/test_theme/test_theme.theme b/core/modules/system/tests/themes/test_theme/test_theme.theme index aa399b6..165c15f 100644 --- a/core/modules/system/tests/themes/test_theme/test_theme.theme +++ b/core/modules/system/tests/themes/test_theme/test_theme.theme @@ -154,3 +154,10 @@ function test_theme_preprocess_theme_test_preprocess_suggestions__kitten__flamin function test_theme_preprocess_theme_test_preprocess_suggestions__kitten__meerkat__tarsier__moose(&$variables) { $variables['bar'] = 'Moose'; } + +/** + * Implements hook_template_preprocess_default_variables_alter(). + */ +function test_theme_template_preprocess_default_variables_alter(array &$variables) { + $variables['default_drink'] = 'coffee'; +}