diff --git a/core/modules/views/tests/src/Functional/ViewsTemplateSuggestionsTest.php b/core/modules/views/tests/src/Functional/ViewsTemplateSuggestionsTest.php new file mode 100644 index 0000000000..86a1dcf350 --- /dev/null +++ b/core/modules/views/tests/src/Functional/ViewsTemplateSuggestionsTest.php @@ -0,0 +1,70 @@ +enableViewsTestModule(); + } + + /** + * Tests for views-view hook suggestions to be loaded. + */ + public function testViewsViewSuggestions() { + // Test with system theme using theme function. + $this->drupalGet('test_page_display_200'); + + // Assert that the default template is loaded. + $this->assertSession()->elementExists('css', '.view.view-id-test_page_display'); + + // Install theme to test with template system. + \Drupal::service('theme_handler')->install(['views_test_suggestions_theme']); + + // Make the theme default then test for hook invocations. + $this->config('system.theme') + ->set('default', 'views_test_suggestions_theme') + ->save(); + $this->assertEquals('views_test_suggestions_theme', $this->config('system.theme')->get('default')); + + $this->drupalGet('test_page_display_200'); + + // Assert template views-view--test-page-display.html.twig is not loaded + // due having less specificity. + $this->assertSession()->pageTextNotContains('**THIS SHOULD NOT BE LOADED**'); + + // Assert that we are using the correct template + // views-view--test-page-display--page-3.html.twig. + $this->assertSession()->pageTextContains('This has been done during SprintWeekend2018 London'); + + // Assert the base template views-view.html.twig is not loaded either. + $this->assertSession()->elementNotExists('css', '.view.view-id-test_page_display'); + } + +} diff --git a/core/modules/views/tests/themes/views_test_suggestions_theme/templates/views-view--test-page-display--page-3.html.twig b/core/modules/views/tests/themes/views_test_suggestions_theme/templates/views-view--test-page-display--page-3.html.twig new file mode 100644 index 0000000000..7cc8e3acbe --- /dev/null +++ b/core/modules/views/tests/themes/views_test_suggestions_theme/templates/views-view--test-page-display--page-3.html.twig @@ -0,0 +1,10 @@ +{# +/** + * @file + * Theme override to display a specific view. + * + * The reason for this template is to override the theme function provided by + * views and validate suggested template are loaded correctly. + */ +#} +This has been done during SprintWeekend2018 London diff --git a/core/modules/views/tests/themes/views_test_suggestions_theme/templates/views-view--test-page-display.html.twig b/core/modules/views/tests/themes/views_test_suggestions_theme/templates/views-view--test-page-display.html.twig new file mode 100644 index 0000000000..a2b0e6ce86 --- /dev/null +++ b/core/modules/views/tests/themes/views_test_suggestions_theme/templates/views-view--test-page-display.html.twig @@ -0,0 +1,10 @@ +{# +/** + * @file + * Theme override to display a specific view. + * + * The reason for this template is to override the theme function provided by + * views and validate suggested template are loaded correctly. + */ +#} +**THIS SHOULD NOT BE LOADED** diff --git a/core/modules/views/tests/themes/views_test_suggestions_theme/views_test_suggestions_theme.info.yml b/core/modules/views/tests/themes/views_test_suggestions_theme/views_test_suggestions_theme.info.yml new file mode 100644 index 0000000000..2594f3f1de --- /dev/null +++ b/core/modules/views/tests/themes/views_test_suggestions_theme/views_test_suggestions_theme.info.yml @@ -0,0 +1,5 @@ +name: Views test suggestions theme +type: theme +description: Theme for testing Views templates suggestions. +version: VERSION +core: 8.x diff --git a/core/modules/views/views.module b/core/modules/views/views.module index 4d4809a5da..2976d6e81e 100644 --- a/core/modules/views/views.module +++ b/core/modules/views/views.module @@ -232,7 +232,7 @@ function views_theme($existing, $type, $theme, $path) { */ function views_theme_suggestions_alter(array &$suggestions, array $variables, $hook) { if (isset($variables['views_plugin'])) { - $suggestions = array_unique(array_merge($suggestions, $variables['views_plugin']->themeFunctions())); + $suggestions = array_reverse(array_unique(array_merge($suggestions, $variables['views_plugin']->themeFunctions()))); } }