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

8.1.x
Nathaniel Catchpole 2016-02-02 09:59:51 +09:00
parent 89d52d9a28
commit e3ad6a4d8b
3 changed files with 40 additions and 1 deletions

View File

@ -77,4 +77,23 @@ class TokenReplaceTest extends ViewKernelTestBase {
}
}
/**
* 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)));
}
}
}

View File

@ -57,3 +57,23 @@ display:
type: views_query
options: { }
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

View File

@ -124,7 +124,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] = (int) ceil(count($view->result) / $per_page);
$replacements[$original] = max(1, (int) ceil(count($view->result) / $per_page));
break;
}
}