diff --git a/core/modules/views/lib/Drupal/views/Plugin/views/area/AreaPluginBase.php b/core/modules/views/lib/Drupal/views/Plugin/views/area/AreaPluginBase.php index 62ac0c8321d87193c4ea33adfab8e11618660eeb..17e7bfa30628b42028922864acd7dab6f310f1a9 100644 --- a/core/modules/views/lib/Drupal/views/Plugin/views/area/AreaPluginBase.php +++ b/core/modules/views/lib/Drupal/views/Plugin/views/area/AreaPluginBase.php @@ -26,6 +26,13 @@ */ abstract class AreaPluginBase extends HandlerBase { + /** + * The type of this area handler, i.e. 'header', 'footer', or 'empty'. + * + * @var string + */ + public $areaType; + /** * Overrides Drupal\views\Plugin\views\HandlerBase::init(). * @@ -35,7 +42,7 @@ abstract class AreaPluginBase extends HandlerBase { public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) { parent::init($view, $display, $options); - if (isset($this->handler_type) && ($this->handler_type == 'empty')) { + if ($this->areaType == 'empty') { $this->options['empty'] = TRUE; } } diff --git a/core/modules/views/lib/Drupal/views/Plugin/views/area/Title.php b/core/modules/views/lib/Drupal/views/Plugin/views/area/Title.php index 61cb8e07ec1cce0d6aba0d26621bdc7e470a3a0b..56950a60f4782c2387562b0af9c42595d1bf6f52 100644 --- a/core/modules/views/lib/Drupal/views/Plugin/views/area/Title.php +++ b/core/modules/views/lib/Drupal/views/Plugin/views/area/Title.php @@ -50,12 +50,13 @@ public function buildOptionsForm(&$form, &$form_state) { * Overrides Drupal\views\Plugin\views\AreaPluginBase::preRender(). */ public function preRender(array $results) { + parent::preRender($results); + + // If a title is provided, process it. if (!empty($this->options['title'])) { $value = $this->globalTokenReplace($this->options['title']); $this->view->setTitle($this->sanitizeValue($value, 'xss_admin'), PASS_THROUGH); } - - return ''; } /** diff --git a/core/modules/views/lib/Drupal/views/Plugin/views/display/DisplayPluginBase.php b/core/modules/views/lib/Drupal/views/Plugin/views/display/DisplayPluginBase.php index 5ae99018150dfc93b5710bef530721d1e36fab9f..8238287bb7da56d1aba5bf5c0a5d6a1e6d53245f 100644 --- a/core/modules/views/lib/Drupal/views/Plugin/views/display/DisplayPluginBase.php +++ b/core/modules/views/lib/Drupal/views/Plugin/views/display/DisplayPluginBase.php @@ -7,6 +7,7 @@ namespace Drupal\views\Plugin\views\display; +use Drupal\views\Plugin\views\area\AreaPluginBase; use Drupal\views\ViewExecutable; use \Drupal\views\Plugin\views\PluginBase; use Drupal\views\Views; @@ -878,8 +879,8 @@ public function getHandlers($type) { $handler = views_get_handler($info['table'], $info['field'], $handler_type, $override); if ($handler) { // Special override for area types so they know where they come from. - if ($handler_type == 'area') { - $handler->handler_type = $type; + if ($handler instanceof AreaPluginBase) { + $handler->areaType = $type; } $handler->init($this->view, $this, $info); diff --git a/core/modules/views/lib/Drupal/views/Tests/Handler/AreaTitleTest.php b/core/modules/views/lib/Drupal/views/Tests/Handler/AreaTitleTest.php new file mode 100644 index 0000000000000000000000000000000000000000..d0d06cf14acbd74eeee9e4c7bfc3534a2d537371 --- /dev/null +++ b/core/modules/views/lib/Drupal/views/Tests/Handler/AreaTitleTest.php @@ -0,0 +1,67 @@ +<?php + +/** + * @file + * Contains \Drupal\views\Tests\Handler\AreaTitleTest. + */ + +namespace Drupal\views\Tests\Handler; + +use Drupal\views\Tests\ViewUnitTestBase; + +/** + * Tests the title area handler. + * + * @see Drupal\views\Plugin\views\area\Title + */ +class AreaTitleTest extends ViewUnitTestBase { + + /** + * Views used by this test. + * + * @var array + */ + public static $testViews = array('test_area_title'); + + public static function getInfo() { + return array( + 'name' => 'Area: Title', + 'description' => 'Tests the title area handler.', + 'group' => 'Views Handlers', + ); + } + + /** + * Tests the title area handler. + */ + public function testTitleText() { + $view = views_get_view('test_area_title'); + + $view->setDisplay('default'); + $this->executeView($view); + $view->render(); + $this->assertFalse($view->getTitle(), 'The title area does not override the title if the view is not empty.'); + $view->destroy(); + + $view->setDisplay('default'); + $this->executeView($view); + $view->result = array(); + $view->render(); + $this->assertEqual($view->getTitle(), 'test_title_empty', 'The title area should override the title if the result is empty.'); + $view->destroy(); + + $view->setDisplay('page_1'); + $this->executeView($view); + $view->render(); + $this->assertEqual($view->getTitle(), 'test_title_header', 'The title area on the header should override the title if the result is not empty.'); + $view->destroy(); + + $view->setDisplay('page_1'); + $this->executeView($view); + $view->result = array(); + $view->render(); + $this->assertEqual($view->getTitle(), 'test_title_header', 'The title area on the header should override the title if the result is empty.'); + $view->destroy(); + } + +} diff --git a/core/modules/views/lib/Drupal/views/ViewExecutable.php b/core/modules/views/lib/Drupal/views/ViewExecutable.php index faa7493411bfa3311e319b18b49322f72421c393..9a4ba482587858db9bc26a9bfdd6dd027bef09d5 100644 --- a/core/modules/views/lib/Drupal/views/ViewExecutable.php +++ b/core/modules/views/lib/Drupal/views/ViewExecutable.php @@ -1287,7 +1287,12 @@ public function render($display_id = NULL) { $this->style_plugin->pre_render($this->result); // Let each area handler have access to the result set. - foreach (array('header', 'footer', 'empty') as $area) { + $areas = array('header', 'footer'); + // Only call preRender() on the empty handlers if the result is empty. + if (empty($this->result)) { + $areas[] = 'empty'; + } + foreach ($areas as $area) { foreach ($this->{$area} as $handler) { $handler->preRender($this->result); } diff --git a/core/modules/views/tests/views_test_config/test_views/views.view.test_area_title.yml b/core/modules/views/tests/views_test_config/test_views/views.view.test_area_title.yml new file mode 100644 index 0000000000000000000000000000000000000000..de78efe870baab419b2fd90568940b1f6ae9c08f --- /dev/null +++ b/core/modules/views/tests/views_test_config/test_views/views.view.test_area_title.yml @@ -0,0 +1,62 @@ +base_table: views_test_data +core: '8' +description: '' +status: '1' +display: + default: + display_options: + defaults: + fields: '0' + pager: '0' + pager_options: '0' + sorts: '0' + fields: + id: + field: id + id: id + relationship: none + table: views_test_data + plugin_id: numeric + pager: + options: + offset: '0' + type: none + pager_options: { } + sorts: + id: + field: id + id: id + order: ASC + relationship: none + table: views_test_data + plugin_id: numeric + empty: + title: + field: title + id: title + table: views + plugin_id: title + title: test_title_empty + display_plugin: default + display_title: Master + id: default + position: '0' + page_1: + display_options: + defaults: + empty: '0' + header: '0' + header: + title: + field: title + id: title + table: views + plugin_id: title + title: test_title_header + display_plugin: page + display_title: Page 1 + id: page_1 + position: '1' +label: '' +id: test_area_title +tag: ''