summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNathaniel Catchpole2016-11-18 11:27:49 (GMT)
committerNathaniel Catchpole2016-11-18 11:28:59 (GMT)
commit8766c9970fcf95c18b56902de951db97ea28982d (patch)
tree4e17a949b1699d6ba755dd74a6b15e10155c533c
parent759c27ce2cb3f1d4e1ea14013e754c504464ade8 (diff)
Issue #2572355 by mikeker, jamesrward, Yogesh Pawar, smk-ka, dawehner, Lendude, DuaelFr, jibran: Some view tokens ([view:page-count] and [view:total-rows]) are incorrect
-rw-r--r--core/modules/views/src/Plugin/views/area/Text.php11
-rw-r--r--core/modules/views/tests/modules/views_test_config/test_views/views.view.test_tokens.yml31
-rw-r--r--core/modules/views/tests/src/Kernel/TokenReplaceTest.php45
-rw-r--r--core/modules/views/views.tokens.inc4
4 files changed, 86 insertions, 5 deletions
diff --git a/core/modules/views/src/Plugin/views/area/Text.php b/core/modules/views/src/Plugin/views/area/Text.php
index cee182f..dab32e9 100644
--- a/core/modules/views/src/Plugin/views/area/Text.php
+++ b/core/modules/views/src/Plugin/views/area/Text.php
@@ -46,6 +46,17 @@ class Text extends TokenizeAreaPluginBase {
/**
* {@inheritdoc}
*/
+ public function preQuery() {
+ $content = $this->options['content']['value'];
+ // Check for tokens that require a total row count.
+ if (strpos($content, '[view:page-count]') !== FALSE || strpos($content, '[view:total-rows]') !== FALSE) {
+ $this->view->get_total_rows = TRUE;
+ }
+ }
+
+ /**
+ * {@inheritdoc}
+ */
public function render($empty = FALSE) {
$format = isset($this->options['content']['format']) ? $this->options['content']['format'] : filter_default_format();
if (!$empty || !empty($this->options['empty'])) {
diff --git a/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_tokens.yml b/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_tokens.yml
index 2bb8216..6032b48 100644
--- a/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_tokens.yml
+++ b/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_tokens.yml
@@ -77,3 +77,34 @@ display:
operator: '='
value: 'not an existing name'
path: test_tokens_empty
+ page_3:
+ display_plugin: page
+ id: page_3
+ display_title: Page
+ position: 1
+ display_options:
+ defaults:
+ title: false
+ pager: false
+ header: false
+ pager:
+ type: mini
+ options:
+ items_per_page: 2
+ title: 'Test token page with minipager'
+ path: test_tokens_minipager
+ display_extenders: { }
+ header:
+ area:
+ id: area
+ table: views
+ field: area
+ relationship: none
+ group_type: group
+ admin_label: ''
+ empty: false
+ tokenize: false
+ content:
+ value: "Total rows: [view:total-rows] - Page count: [view:page-count]"
+ format: basic_html
+ plugin_id: text
diff --git a/core/modules/views/tests/src/Kernel/TokenReplaceTest.php b/core/modules/views/tests/src/Kernel/TokenReplaceTest.php
index 5cf40ad..160f4e2 100644
--- a/core/modules/views/tests/src/Kernel/TokenReplaceTest.php
+++ b/core/modules/views/tests/src/Kernel/TokenReplaceTest.php
@@ -33,6 +33,8 @@ class TokenReplaceTest extends ViewsKernelTestBase {
$token_handler = \Drupal::token();
$view = Views::getView('test_tokens');
$view->setDisplay('page_1');
+ // Force the view to span more than one page to better test page_count.
+ $view->display_handler->getPlugin('pager')->setItemsPerPage(4);
$this->executeView($view);
$expected = array(
@@ -41,12 +43,12 @@ class TokenReplaceTest extends ViewsKernelTestBase {
'[view:id]' => 'test_tokens',
'[view:title]' => 'Test token page',
'[view:url]' => $view->getUrl(NULL, 'page_1')->setAbsolute(TRUE)->toString(),
- '[view:total-rows]' => (string) $view->total_rows,
+ '[view:total-rows]' => '5',
'[view:base-table]' => 'views_test_data',
'[view:base-field]' => 'id',
- '[view:items-per-page]' => '10',
+ '[view:items-per-page]' => '4',
'[view:current-page]' => '1',
- '[view:page-count]' => '1',
+ '[view:page-count]' => '2',
);
$base_bubbleable_metadata = BubbleableMetadata::createFromObject($view->storage);
@@ -72,6 +74,43 @@ class TokenReplaceTest extends ViewsKernelTestBase {
}
/**
+ * Tests core token replacements generated from a view.
+ */
+ function testTokenReplacementWithMiniPager() {
+ $token_handler = \Drupal::token();
+ $view = Views::getView('test_tokens');
+ $view->setDisplay('page_3');
+ $this->executeView($view);
+
+ $this->assertSame(TRUE, $view->get_total_rows, 'The query was set to calculate the total number of rows.');
+
+ $expected = array(
+ '[view:label]' => 'Test tokens',
+ '[view:description]' => 'Test view to token replacement tests.',
+ '[view:id]' => 'test_tokens',
+ '[view:title]' => 'Test token page with minipager',
+ '[view:url]' => $view->getUrl(NULL, 'page_3')
+ ->setAbsolute(TRUE)
+ ->toString(),
+ '[view:total-rows]' => '5',
+ '[view:base-table]' => 'views_test_data',
+ '[view:base-field]' => 'id',
+ '[view:items-per-page]' => '2',
+ '[view:current-page]' => '1',
+ '[view:page-count]' => '3',
+ );
+
+ $base_bubbleable_metadata = BubbleableMetadata::createFromObject($view->storage);
+
+ foreach ($expected as $token => $expected_output) {
+ $bubbleable_metadata = new BubbleableMetadata();
+ $output = $token_handler->replace($token, array('view' => $view), [], $bubbleable_metadata);
+ $this->assertSame($expected_output, $output, sprintf('Token %s replaced correctly.', $token));
+ $this->assertEquals($base_bubbleable_metadata, $bubbleable_metadata);
+ }
+ }
+
+ /**
* Tests core token replacements generated from a view without results.
*/
function testTokenReplacementNoResults() {
diff --git a/core/modules/views/views.tokens.inc b/core/modules/views/views.tokens.inc
index a092955..defd34f 100644
--- a/core/modules/views/views.tokens.inc
+++ b/core/modules/views/views.tokens.inc
@@ -119,7 +119,7 @@ function views_tokens($type, $tokens, array $data, array $options, BubbleableMet
$replacements[$original] = $view->storage->get('base_field');
break;
case 'total-rows':
- $replacements[$original] = count($view->result);
+ $replacements[$original] = (int) $view->total_rows;
break;
case 'items-per-page':
$replacements[$original] = (int) $view->getItemsPerPage();
@@ -130,7 +130,7 @@ function views_tokens($type, $tokens, array $data, array $options, BubbleableMet
case 'page-count':
// If there are no items per page, set this to 1 for the division.
$per_page = $view->getItemsPerPage() ?: 1;
- $replacements[$original] = max(1, (int) ceil(count($view->result) / $per_page));
+ $replacements[$original] = max(1, (int) ceil($view->total_rows / $per_page));
break;
}
}