diff --git a/core/modules/views/src/Tests/TokenReplaceTest.php b/core/modules/views/src/Tests/TokenReplaceTest.php index 0462756b8d34d80d55997c8e7b607c821ba5fd23..149280f0808bcf75cde7ce67a938d0977191febf 100644 --- a/core/modules/views/src/Tests/TokenReplaceTest.php +++ b/core/modules/views/src/Tests/TokenReplaceTest.php @@ -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))); + } + } + } 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 bf9341b9cbdbebd4b8de8c685bca54e5144fea06..2bb82167fe08ac32a86440035720ffa06ea62a0c 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 @@ -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 diff --git a/core/modules/views/views.tokens.inc b/core/modules/views/views.tokens.inc index 6d9de956fc316466acdad70971aa219be8358e41..59faec39b46323b914ec6b261cb020354d046151 100644 --- a/core/modules/views/views.tokens.inc +++ b/core/modules/views/views.tokens.inc @@ -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; } }