From aa13a04174869c662e7112916341a64e148bb5d9 Mon Sep 17 00:00:00 2001 From: Florent Torregrosa Date: Sat, 3 Sep 2016 17:58:58 +0200 Subject: [PATCH] Issue #2782221 by Grimreaper: Result summary Area plugin not displayed when there is no result --- .../modules/views/src/Plugin/views/area/Result.php | 23 +++--- .../test_views/views.view.test_area_result.yml | 79 ++++++++++++++++++++ .../tests/src/Kernel/Handler/AreaResultTest.php | 86 ++++++++++++++++++++++ 3 files changed, 177 insertions(+), 11 deletions(-) create mode 100644 core/modules/views/tests/modules/views_test_config/test_views/views.view.test_area_result.yml create mode 100644 core/modules/views/tests/src/Kernel/Handler/AreaResultTest.php diff --git a/core/modules/views/src/Plugin/views/area/Result.php b/core/modules/views/src/Plugin/views/area/Result.php index d8139cb..c2511d5 100644 --- a/core/modules/views/src/Plugin/views/area/Result.php +++ b/core/modules/views/src/Plugin/views/area/Result.php @@ -74,7 +74,6 @@ public function render($empty = FALSE) { if (!isset($this->options['content']) || $this->view->style_plugin instanceof DefaultSummary) { return array(); } - $output = ''; $format = $this->options['content']; // Calculate the page totals. $current_page = (int) $this->view->getCurrentPage() + 1; @@ -83,9 +82,11 @@ public function render($empty = FALSE) { // Not every view has total_rows set, use view->result instead. $total = isset($this->view->total_rows) ? $this->view->total_rows : count($this->view->result); $label = Html::escape($this->view->storage->label()); + // Offset for start calculation. + $start_offset = empty($total) ? 0 : 1; if ($per_page === 0) { $page_count = 1; - $start = 1; + $start = $start_offset; $end = $total; } else { @@ -94,10 +95,10 @@ public function render($empty = FALSE) { if ($total_count > $total) { $total_count = $total; } - $start = ($current_page - 1) * $per_page + 1; + $start = ($current_page - 1) * $per_page + $start_offset; $end = $total_count; } - $current_record_count = ($end - $start) + 1; + $current_record_count = ($end - $start) + $start_offset; // Get the search information. $replacements = []; $replacements['@start'] = $start; @@ -108,14 +109,14 @@ public function render($empty = FALSE) { $replacements['@current_page'] = $current_page; $replacements['@current_record_count'] = $current_record_count; $replacements['@page_count'] = $page_count; - // Send the output. - if (!empty($total)) { - $output .= Xss::filterAdmin(str_replace(array_keys($replacements), array_values($replacements), $format)); - } // Return as render array. - return array( - '#markup' => $output, - ); + if (!empty($total) || !empty($this->options['empty'])) { + return array( + '#markup' => Xss::filterAdmin(str_replace(array_keys($replacements), array_values($replacements), $format)), + ); + } + + return array(); } } diff --git a/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_area_result.yml b/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_area_result.yml new file mode 100644 index 0000000..8ca5628 --- /dev/null +++ b/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_area_result.yml @@ -0,0 +1,79 @@ +langcode: en +status: true +dependencies: { } +id: test_area_result +label: '' +module: views +description: '' +tag: '' +base_table: views_test_data +base_field: nid +core: '8' +display: + default: + display_options: + defaults: + fields: false + pager: false + sorts: false + fields: + id: + field: id + id: id + relationship: none + table: views_test_data + plugin_id: numeric + pager: + options: + offset: 0 + type: none + 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 + header: + result: + id: result + table: views + field: result + relationship: none + group_type: group + admin_label: '' + empty: true + content: "start: @start | end: @end | total: @total | label: @label | per page: @per_page | current page: @current_page | current record count: @current_record_count | page count: @page_count" + plugin_id: result + display_plugin: default + display_title: Master + id: default + position: 0 + page_1: + display_options: + path: test-area-result + defaults: + header: false + header: + result: + id: result + table: views + field: result + relationship: none + group_type: group + admin_label: '' + empty: false + content: "start: @start | end: @end | total: @total | label: @label | per page: @per_page | current page: @current_page | current record count: @current_record_count | page count: @page_count" + plugin_id: result + display_plugin: page + display_title: 'Page 1' + id: page_1 + position: 1 diff --git a/core/modules/views/tests/src/Kernel/Handler/AreaResultTest.php b/core/modules/views/tests/src/Kernel/Handler/AreaResultTest.php new file mode 100644 index 0000000..8e890bf --- /dev/null +++ b/core/modules/views/tests/src/Kernel/Handler/AreaResultTest.php @@ -0,0 +1,86 @@ +setDisplay('default'); + $this->executeView($view); + $output = $view->render(); + $output = \Drupal::service('renderer')->renderRoot($output); + $this->setRawContent($output); + $this->assertText('start: 1 | end: 5 | total: 5 | label: test_area_result | per page: 0 | current page: 1 | current record count: 5 | page count: 1'); + } + + /** + * Tests the messages area handler. + */ + public function testResultEmpty() { + $view = Views::getView('test_area_result'); + + // Test that the area is displayed if we have checked the empty checkbox. + $view->setDisplay('default'); + + // Add a filtering to have no result. + $view->displayHandlers->get('default')->overrideOption('filters', array( + 'name' => array( + 'id' => 'name', + 'table' => 'views_test_data', + 'field' => 'name', + 'relationship' => 'none', + 'operator' => '=', + 'value' => 'non-existing-name', + ), + )); + + $this->executeView($view); + $output = $view->render(); + $output = \Drupal::service('renderer')->renderRoot($output); + $this->setRawContent($output); + $this->assertText('start: 0 | end: 0 | total: 0 | label: test_area_result | per page: 0 | current page: 1 | current record count: 0 | page count: 1'); + + // Test that the area is not displayed if we have not checked the empty + // checkbox. + $view->setDisplay('page_1'); + + // Add a filtering to have no result. + $view->displayHandlers->get('page_1')->overrideOption('filters', array( + 'name' => array( + 'id' => 'name', + 'table' => 'views_test_data', + 'field' => 'name', + 'relationship' => 'none', + 'operator' => '=', + 'value' => 'non-existing-name', + ), + )); + + $this->executeView($view); + $output = $view->render(); + $output = \Drupal::service('renderer')->renderRoot($output); + $this->setRawContent($output); + $this->assertNoText('start: 0 | end: 0 | total: 0 | label: test_area_result | per page: 0 | current page: 1 | current record count: 0 | page count: 1'); + } + +} -- 2.1.4