Skip to content
Snippets Groups Projects
Commit e3ad6a4d authored by catch's avatar catch
Browse files

Issue #2510076 by DuaelFr: The [view:page-count] token should never return 0

parent 89d52d9a
No related branches found
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
...@@ -77,4 +77,23 @@ function testTokenReplacement() { ...@@ -77,4 +77,23 @@ function testTokenReplacement() {
} }
} }
/**
* Tests core token replacements generated from a view without results.
*/
function testTokenReplacementNoResults() {
$token_handler = \Drupal::token();
$view = Views::getView('test_tokens');
$view->setDisplay('page_2');
$this->executeView($view);
$expected = array(
'[view:page-count]' => '1',
);
foreach ($expected as $token => $expected_output) {
$output = $token_handler->replace($token, array('view' => $view));
$this->assertIdentical($output, $expected_output, format_string('Token %token replaced correctly.', array('%token' => $token)));
}
}
} }
...@@ -57,3 +57,23 @@ display: ...@@ -57,3 +57,23 @@ display:
type: views_query type: views_query
options: { } options: { }
path: test_tokens path: test_tokens
page_2:
id: page_2
display_title: Page
display_plugin: page
position: 2
display_options:
defaults:
filters: false
query:
type: views_query
options: { }
filters:
name:
field: name
id: test_filter
table: views_test_data
plugin_id: string
operator: '='
value: 'not an existing name'
path: test_tokens_empty
...@@ -124,7 +124,7 @@ function views_tokens($type, $tokens, array $data, array $options, BubbleableMet ...@@ -124,7 +124,7 @@ function views_tokens($type, $tokens, array $data, array $options, BubbleableMet
case 'page-count': case 'page-count':
// If there are no items per page, set this to 1 for the division. // If there are no items per page, set this to 1 for the division.
$per_page = $view->getItemsPerPage() ?: 1; $per_page = $view->getItemsPerPage() ?: 1;
$replacements[$original] = (int) ceil(count($view->result) / $per_page); $replacements[$original] = max(1, (int) ceil(count($view->result) / $per_page));
break; break;
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment