From bcbe3c63a3451f39e597b03b128e8e5a80285dae Mon Sep 17 00:00:00 2001 From: Alex Pott <alex.a.pott@googlemail.com> Date: Tue, 18 Aug 2015 17:10:18 +0100 Subject: [PATCH] Issue #2412167 by geertvd, dawehner, Sutharsan: hook_views_post_render fails in a theme --- .../tests/themes/test_basetheme/test_basetheme.theme | 3 ++- .../tests/themes/test_subtheme/test_subtheme.theme | 6 +++++- core/modules/views/src/Tests/ViewsHooksTest.php | 6 ++++++ .../views/src/Tests/ViewsThemeIntegrationTest.php | 6 ++++-- core/modules/views/src/ViewExecutable.php | 10 ++++------ .../views_test_data.views_execution.inc | 3 ++- 6 files changed, 23 insertions(+), 11 deletions(-) diff --git a/core/modules/system/tests/themes/test_basetheme/test_basetheme.theme b/core/modules/system/tests/themes/test_basetheme/test_basetheme.theme index 739439f88bea..986428693baf 100644 --- a/core/modules/system/tests/themes/test_basetheme/test_basetheme.theme +++ b/core/modules/system/tests/themes/test_basetheme/test_basetheme.theme @@ -4,6 +4,7 @@ * @file * Add hooks for tests to use. */ +use Drupal\views\Plugin\views\cache\CachePluginBase; use Drupal\views\ViewExecutable; /** @@ -17,7 +18,7 @@ function test_basetheme_views_pre_render(ViewExecutable $view) { /** * Implements hook_views_post_render(). */ -function test_basetheme_views_post_render(ViewExecutable $view) { +function test_basetheme_views_post_render(ViewExecutable $view, &$output, CachePluginBase $cache) { // We append the function name to the title for test to check for. $view->setTitle($view->getTitle() . ":" . __FUNCTION__); } diff --git a/core/modules/system/tests/themes/test_subtheme/test_subtheme.theme b/core/modules/system/tests/themes/test_subtheme/test_subtheme.theme index 0e359103f1bd..cd312dbcb7d7 100644 --- a/core/modules/system/tests/themes/test_subtheme/test_subtheme.theme +++ b/core/modules/system/tests/themes/test_subtheme/test_subtheme.theme @@ -5,6 +5,7 @@ * Add hooks for tests to use. */ +use Drupal\views\Plugin\views\cache\CachePluginBase; use Drupal\views\ViewExecutable; /** @@ -18,9 +19,12 @@ function test_subtheme_views_pre_render(ViewExecutable $view) { /** * Implements hook_views_post_render(). */ -function test_subtheme_views_post_render(ViewExecutable $view) { +function test_subtheme_views_post_render(ViewExecutable $view, &$output, CachePluginBase $cache) { // We append the function name to the title for test to check for. $view->setTitle($view->getTitle() . ":" . __FUNCTION__); + if ($view->id() == 'test_page_display') { + $output['#rows'][0]['#title'] = t('%total_rows items found.', array('%total_rows' => $view->total_rows)); + } } /** diff --git a/core/modules/views/src/Tests/ViewsHooksTest.php b/core/modules/views/src/Tests/ViewsHooksTest.php index 2ed63e0021e0..a0e9583968ce 100644 --- a/core/modules/views/src/Tests/ViewsHooksTest.php +++ b/core/modules/views/src/Tests/ViewsHooksTest.php @@ -65,11 +65,17 @@ protected function setUp() { */ public function testHooks() { $view = Views::getView('test_view'); + $view->setDisplay(); // Test each hook is found in the implementations array and is invoked. foreach (static::$hooks as $hook => $type) { $this->assertTrue($this->moduleHandler->implementsHook('views_test_data', $hook), format_string('The hook @hook was registered.', array('@hook' => $hook))); + if ($hook == 'views_post_render') { + $this->moduleHandler->invoke('views_test_data', $hook, array($view, &$view->display_handler->output, $view->display_handler->getPlugin('cache'))); + continue; + } + switch ($type) { case 'view': $this->moduleHandler->invoke('views_test_data', $hook, array($view)); diff --git a/core/modules/views/src/Tests/ViewsThemeIntegrationTest.php b/core/modules/views/src/Tests/ViewsThemeIntegrationTest.php index 91de60ce25ef..f6664e019b53 100644 --- a/core/modules/views/src/Tests/ViewsThemeIntegrationTest.php +++ b/core/modules/views/src/Tests/ViewsThemeIntegrationTest.php @@ -74,11 +74,13 @@ public function testThemedViewPage() { // Make sure a views rendered page is touched. $this->drupalGet('test_page_display_200'); - $this->assertRaw("test_subtheme_views_pre_render", "Views title changed by test_usetheme.test_subtheme_views_pre_render"); - $this->assertRaw("test_subtheme_views_post_render", "Views title changed by test_usetheme.test_subtheme_views_post_render"); + $this->assertRaw("test_subtheme_views_pre_render", "Views title changed by test_subtheme.test_subtheme_views_pre_render"); + $this->assertRaw("test_subtheme_views_post_render", "Views title changed by test_subtheme.test_subtheme_views_post_render"); $this->assertRaw("test_basetheme_views_pre_render", "Views title changed by test_basetheme.test_basetheme_views_pre_render"); $this->assertRaw("test_basetheme_views_post_render", "Views title changed by test_basetheme.test_basetheme_views_post_render"); + + $this->assertRaw('<em class="placeholder">' . count($this->dataSet()) . '</em> items found.', 'Views group title added by test_subtheme.test_subtheme_views_post_render'); } } diff --git a/core/modules/views/src/ViewExecutable.php b/core/modules/views/src/ViewExecutable.php index 774e63cf5caf..2a8145c58591 100644 --- a/core/modules/views/src/ViewExecutable.php +++ b/core/modules/views/src/ViewExecutable.php @@ -1371,11 +1371,11 @@ public function render($display_id = NULL) { $themes[] = $active_theme->getName(); // Check for already-cached output. + /** @var \Drupal\views\Plugin\views\cache\CachePluginBase $cache */ if (!empty($this->live_preview)) { - $cache = FALSE; + $cache = Views::pluginManager('cache')->createInstance('none'); } else { - /** @var \Drupal\views\Plugin\views\cache\CachePluginBase $cache */ $cache = $this->display_handler->getPlugin('cache'); } @@ -1431,9 +1431,7 @@ public function render($display_id = NULL) { $exposed_form->postRender($this->display_handler->output); - if ($cache) { - $cache->postRender($this->display_handler->output); - } + $cache->postRender($this->display_handler->output); // Let modules modify the view output after it is rendered. $module_handler->invokeAll('views_post_render', array($this, &$this->display_handler->output, $cache)); @@ -1442,7 +1440,7 @@ public function render($display_id = NULL) { foreach ($themes as $theme_name) { $function = $theme_name . '_views_post_render'; if (function_exists($function)) { - $function($this); + $function($this, $this->display_handler->output, $cache); } } diff --git a/core/modules/views/tests/modules/views_test_data/views_test_data.views_execution.inc b/core/modules/views/tests/modules/views_test_data/views_test_data.views_execution.inc index 16984952d7e0..960fc8f57ae5 100644 --- a/core/modules/views/tests/modules/views_test_data/views_test_data.views_execution.inc +++ b/core/modules/views/tests/modules/views_test_data/views_test_data.views_execution.inc @@ -6,6 +6,7 @@ */ use Drupal\field\FieldStorageConfigInterface; +use Drupal\views\Plugin\views\cache\CachePluginBase; use Drupal\views\ViewExecutable; /** @@ -66,7 +67,7 @@ function views_test_data_placeholders() { /** * Implements hook_views_post_render(). */ -function views_test_data_views_post_render(ViewExecutable $view) { +function views_test_data_views_post_render(ViewExecutable $view, &$output, CachePluginBase $cache) { \Drupal::state()->set('views_hook_test_views_post_render', TRUE); } -- GitLab