diff --git a/core/modules/system/lib/Drupal/system/Tests/Theme/ThemeSuggestionsAlterTest.php b/core/modules/system/lib/Drupal/system/Tests/Theme/ThemeSuggestionsAlterTest.php index 6ded5e5..8f6e9b6 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Theme/ThemeSuggestionsAlterTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Theme/ThemeSuggestionsAlterTest.php @@ -52,24 +52,45 @@ function testTemplateSuggestions() { } /** + * Tests hook_theme_suggestions_alter(). + */ + function testGeneralSuggestionsAlter() { + $this->drupalGet('theme-test/general-suggestion-alter'); + $this->assertText('Original template for testing hook_theme_suggestions_alter().'); + + // Enable test_theme and test that themes can alter template suggestions. + config('system.theme') + ->set('default', 'test_theme') + ->save(); + $this->drupalGet('theme-test/general-suggestion-alter'); + $this->assertText('Template overridden based on new theme suggestion provided by the test_theme theme via hook_theme_suggestions_alter().'); + + // Enable the theme_suggestions_test module to test modules implementing + // suggestions alter hooks. + \Drupal::moduleHandler()->install(array('theme_suggestions_test')); + $this->drupalGet('theme-test/general-suggestion-alter'); + $this->assertText('Template overridden based on new theme suggestion provided by a module via hook_theme_suggestions_alter().'); + } + + /** * Tests that theme suggestion alter hooks work for templates. */ function testTemplateSuggestionsAlter() { $this->drupalGet('theme-test/suggestion-alter'); - $this->assertText('Original template.'); + $this->assertText('Original template for testing hook_theme_suggestions_HOOK_alter().'); // Enable test_theme and test that themes can alter template suggestions. config('system.theme') ->set('default', 'test_theme') ->save(); $this->drupalGet('theme-test/suggestion-alter'); - $this->assertText('Template overridden based on new theme suggestion provided by the test_theme theme.'); + $this->assertText('Template overridden based on new theme suggestion provided by the test_theme theme via hook_theme_suggestions_HOOK_alter().'); // Enable the theme_suggestions_test module to test modules implementing // suggestions alter hooks. \Drupal::moduleHandler()->install(array('theme_suggestions_test')); $this->drupalGet('theme-test/suggestion-alter'); - $this->assertText('Template overridden based on new theme suggestion provided by a module.'); + $this->assertText('Template overridden based on new theme suggestion provided by a module via hook_theme_suggestions_HOOK_alter().'); } /** @@ -133,7 +154,6 @@ function testExecutionOrder() { // Send two requests so that we get all the messages we've set via // drupal_set_message(). $this->drupalGet('theme-test/suggestion-alter'); - $this->drupalGet('theme-test/suggestion-alter'); // Ensure that the order is first by extension, then for a given extension, // the hook-specific one after the generic one. $expected = array( diff --git a/core/modules/system/tests/modules/theme_suggestions_test/theme_suggestions_test.module b/core/modules/system/tests/modules/theme_suggestions_test/theme_suggestions_test.module index 690422e..1b19fee 100644 --- a/core/modules/system/tests/modules/theme_suggestions_test/theme_suggestions_test.module +++ b/core/modules/system/tests/modules/theme_suggestions_test/theme_suggestions_test.module @@ -10,6 +10,9 @@ */ function theme_suggestions_test_theme_suggestions_alter(array &$suggestions, array $variables, $hook) { drupal_set_message(__FUNCTION__ . '() executed.'); + if ($hook == 'theme_test_general_suggestions') { + $suggestions[] = $hook . '__module_override'; + } } /** diff --git a/core/modules/system/tests/modules/theme_test/lib/Drupal/theme_test/ThemeTestController.php b/core/modules/system/tests/modules/theme_test/lib/Drupal/theme_test/ThemeTestController.php index 8da6c62..29bfa4e 100644 --- a/core/modules/system/tests/modules/theme_test/lib/Drupal/theme_test/ThemeTestController.php +++ b/core/modules/system/tests/modules/theme_test/lib/Drupal/theme_test/ThemeTestController.php @@ -107,6 +107,13 @@ function suggestionAlter() { } /** + * Menu callback for testing hook_theme_suggestions_alter(). + */ + function generalSuggestionAlter() { + return array('#theme' => 'theme_test_general_suggestions'); + } + + /** * Menu callback for testing suggestion alter hooks with specific suggestions. */ function specificSuggestionAlter() { diff --git a/core/modules/system/tests/modules/theme_test/templates/theme-test-general-suggestions.html.twig b/core/modules/system/tests/modules/theme_test/templates/theme-test-general-suggestions.html.twig new file mode 100644 index 0000000..086eb47 --- /dev/null +++ b/core/modules/system/tests/modules/theme_test/templates/theme-test-general-suggestions.html.twig @@ -0,0 +1,2 @@ +{# Output for Theme API test #} +Original template for testing hook_theme_suggestions_alter(). diff --git a/core/modules/system/tests/modules/theme_test/templates/theme-test-suggestions.html.twig b/core/modules/system/tests/modules/theme_test/templates/theme-test-suggestions.html.twig index dfc848c..42bb3b9 100644 --- a/core/modules/system/tests/modules/theme_test/templates/theme-test-suggestions.html.twig +++ b/core/modules/system/tests/modules/theme_test/templates/theme-test-suggestions.html.twig @@ -1,2 +1,2 @@ {# Output for Theme API test #} -Original template. +Original template for testing hook_theme_suggestions_HOOK_alter(). 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 ddf63de..bdb1404 100644 --- a/core/modules/system/tests/modules/theme_test/theme_test.module +++ b/core/modules/system/tests/modules/theme_test/theme_test.module @@ -26,6 +26,10 @@ function theme_test_theme($existing, $type, $theme, $path) { 'template' => 'theme-test-suggestions', 'variables' => array(), ); + $items['theme_test_general_suggestions'] = array( + 'template' => 'theme-test-general-suggestions', + 'variables' => array(), + ); $items['theme_test_function_suggestions'] = array( 'variables' => array(), ); diff --git a/core/modules/system/tests/modules/theme_test/theme_test.routing.yml b/core/modules/system/tests/modules/theme_test/theme_test.routing.yml index 9f08d5b..6b78b13 100644 --- a/core/modules/system/tests/modules/theme_test/theme_test.routing.yml +++ b/core/modules/system/tests/modules/theme_test/theme_test.routing.yml @@ -65,6 +65,13 @@ suggestion_alter: requirements: _permission: 'access content' +theme_test.general_suggestion_alter: + path: '/theme-test/general-suggestion-alter' + defaults: + _content: '\Drupal\theme_test\ThemeTestController::generalSuggestionAlter' + requirements: + _permission: 'access content' + suggestion_provided: path: '/theme-test/suggestion-provided' defaults: diff --git a/core/modules/system/tests/themes/test_theme/templates/theme-test-suggestions--module-override.html.twig b/core/modules/system/tests/themes/test_theme/templates/theme-test-general-suggestions--module-override.html.twig similarity index 69% copy from core/modules/system/tests/themes/test_theme/templates/theme-test-suggestions--module-override.html.twig copy to core/modules/system/tests/themes/test_theme/templates/theme-test-general-suggestions--module-override.html.twig index 26ce57b..5969332 100644 --- a/core/modules/system/tests/themes/test_theme/templates/theme-test-suggestions--module-override.html.twig +++ b/core/modules/system/tests/themes/test_theme/templates/theme-test-general-suggestions--module-override.html.twig @@ -1,2 +1,2 @@ {# Output for Theme API test #} -Template overridden based on new theme suggestion provided by a module. +Template overridden based on new theme suggestion provided by a module via hook_theme_suggestions_alter(). diff --git a/core/modules/system/tests/themes/test_theme/templates/theme-test-suggestions--theme-override.html.twig b/core/modules/system/tests/themes/test_theme/templates/theme-test-general-suggestions--theme-override.html.twig similarity index 63% copy from core/modules/system/tests/themes/test_theme/templates/theme-test-suggestions--theme-override.html.twig copy to core/modules/system/tests/themes/test_theme/templates/theme-test-general-suggestions--theme-override.html.twig index dee829f..29322da 100644 --- a/core/modules/system/tests/themes/test_theme/templates/theme-test-suggestions--theme-override.html.twig +++ b/core/modules/system/tests/themes/test_theme/templates/theme-test-general-suggestions--theme-override.html.twig @@ -1,2 +1,2 @@ {# Output for Theme API test #} -Template overridden based on new theme suggestion provided by the test_theme theme. +Template overridden based on new theme suggestion provided by the test_theme theme via hook_theme_suggestions_alter(). diff --git a/core/modules/system/tests/themes/test_theme/templates/theme-test-suggestions--module-override.html.twig b/core/modules/system/tests/themes/test_theme/templates/theme-test-suggestions--module-override.html.twig index 26ce57b..2a005cb 100644 --- a/core/modules/system/tests/themes/test_theme/templates/theme-test-suggestions--module-override.html.twig +++ b/core/modules/system/tests/themes/test_theme/templates/theme-test-suggestions--module-override.html.twig @@ -1,2 +1,2 @@ {# Output for Theme API test #} -Template overridden based on new theme suggestion provided by a module. +Template overridden based on new theme suggestion provided by a module via hook_theme_suggestions_HOOK_alter(). diff --git a/core/modules/system/tests/themes/test_theme/templates/theme-test-suggestions--theme-override.html.twig b/core/modules/system/tests/themes/test_theme/templates/theme-test-suggestions--theme-override.html.twig index dee829f..4516238 100644 --- a/core/modules/system/tests/themes/test_theme/templates/theme-test-suggestions--theme-override.html.twig +++ b/core/modules/system/tests/themes/test_theme/templates/theme-test-suggestions--theme-override.html.twig @@ -1,2 +1,2 @@ {# Output for Theme API test #} -Template overridden based on new theme suggestion provided by the test_theme theme. +Template overridden based on new theme suggestion provided by the test_theme theme via hook_theme_suggestions_HOOK_alter(). 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 df30618..13cb2b9 100644 --- a/core/modules/system/tests/themes/test_theme/test_theme.theme +++ b/core/modules/system/tests/themes/test_theme/test_theme.theme @@ -35,6 +35,13 @@ function test_theme_theme_test_alter_alter(&$data) { */ function test_theme_theme_suggestions_alter(array &$suggestions, array $variables, $hook) { drupal_set_message(__FUNCTION__ . '() executed.'); + // Theme alter hooks run after module alter hooks, so add this theme + // suggestion to the beginning of the array so that the suggestion added by + // the theme_suggestions_test module can be picked up when that module is + // enabled. + if ($hook == 'theme_test_general_suggestions') { + array_unshift($suggestions, 'theme_test_general_suggestions__' . 'theme_override'); + } } /** diff --git a/core/modules/system/theme.api.php b/core/modules/system/theme.api.php index f8fdda4..db11a21 100644 --- a/core/modules/system/theme.api.php +++ b/core/modules/system/theme.api.php @@ -209,7 +209,7 @@ function hook_theme_suggestions_HOOK(array $variables) { * node and taxonomy term templates based on the user being logged in. * @code * function MYMODULE_theme_suggestions_alter(array &$suggestions, array $variables, $hook) { - * if ($variables['logged_in'] && in_array($hook, array('node', 'taxonomy-term'))) { + * if (\Drupal::currentUser()->isAuthenticated() && in_array($hook, array('node', 'taxonomy_term'))) { * $suggestions[] = $hook . '__' . 'logged_in'; * } * } @@ -233,8 +233,9 @@ function hook_theme_suggestions_HOOK(array $variables) { * * @see hook_theme_suggestions_HOOK_alter() */ -function hook_theme_suggestions_alter(array $suggestions, array $variables, $hook) { - $suggestions[] = $hook . '__' . $variables['elements']['#langcode']; +function hook_theme_suggestions_alter(array &$suggestions, array $variables, $hook) { + // Add an interface-language specific suggestion to all theme hooks. + $suggestions[] = $hook . '__' . \Drupal::languageManager()->getLanguage()->id; } /**