Commit 8766c997 authored by catch's avatar catch

Issue #2572355 by mikeker, jamesrward, Yogesh Pawar, smk-ka, dawehner,...

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
parent 759c27ce
......@@ -43,6 +43,17 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) {
);
}
/**
* {@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}
*/
......
......@@ -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
......@@ -33,6 +33,8 @@ function testTokenReplacement() {
$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 @@ function testTokenReplacement() {
'[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);
......@@ -71,6 +73,43 @@ function testTokenReplacement() {
}
}
/**
* 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.
*/
......
......@@ -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;
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment