diff --git a/core/includes/pager.inc b/core/includes/pager.inc index 62e8278..c4bce85 100644 --- a/core/includes/pager.inc +++ b/core/includes/pager.inc @@ -29,9 +29,12 @@ function pager_find_page($element = 0) { $page = \Drupal::request()->query->get('page', ''); $page_array = explode(',', $page); if (!isset($page_array[$element])) { - $page_array[$element] = 0; + $page_found = 0; } - return (int) $page_array[$element]; + else { + $page_found = max(0, (int) $page_array[$element]); + } + return $page_found; } /** diff --git a/core/lib/Drupal/Core/Entity/Query/QueryBase.php b/core/lib/Drupal/Core/Entity/Query/QueryBase.php index ed3d12b..1d4cbb1 100644 --- a/core/lib/Drupal/Core/Entity/Query/QueryBase.php +++ b/core/lib/Drupal/Core/Entity/Query/QueryBase.php @@ -291,9 +291,9 @@ public function pager($limit = 10, $element = NULL) { */ protected function initializePager() { if ($this->pager && !empty($this->pager['limit']) && !$this->count) { - $page = pager_find_page($this->pager['element']); $count_query = clone $this; $this->pager['total'] = $count_query->count()->execute(); + $page = max(0, min(pager_find_page($this->pager['element']), ((int) ceil($this->pager['total'] / $this->pager['limit'])) - 1)); $this->pager['start'] = $page * $this->pager['limit']; pager_default_initialize($this->pager['total'], $this->pager['limit'], $this->pager['element']); $this->range($this->pager['start'], $this->pager['limit']); diff --git a/core/modules/system/src/Tests/Pager/PagerTest.php b/core/modules/system/src/Tests/Pager/PagerTest.php index 71413f2..0a49a58 100644 --- a/core/modules/system/src/Tests/Pager/PagerTest.php +++ b/core/modules/system/src/Tests/Pager/PagerTest.php @@ -120,6 +120,13 @@ public function testMultiplePagers() { 'expected_page' => [0 => '3', 1 => '4', 4 => '5'], 'expected_query' => '?page=2,3,,,4', ], + // If negative values are passed as page numbers, the first page is + // returned. + [ + 'input_query' => '?page=-2,-6,,,5', + 'expected_page' => [0 => '1', 1 => '1', 4 => '6'], + 'expected_query' => '?page=0,0,,,5', + ], // If floats are passed as page numbers, only the integer part is // returned. [