diff --git a/core/lib/Drupal/Core/Pager/PagerManager.php b/core/lib/Drupal/Core/Pager/PagerManager.php index 68827d2..1a3dabd 100644 --- a/core/lib/Drupal/Core/Pager/PagerManager.php +++ b/core/lib/Drupal/Core/Pager/PagerManager.php @@ -58,6 +58,13 @@ public function getPager($element = 0) { return isset($this->pagers[$element]) ? $this->pagers[$element] : NULL; } + /** + * {@inheritdoc} + */ + public function findPage($pager_id = 0): int { + return $this->pagerParams->findPage($pager_id); + } + /** * {@inheritdoc} */ diff --git a/core/lib/Drupal/Core/Pager/PagerManagerInterface.php b/core/lib/Drupal/Core/Pager/PagerManagerInterface.php index 27dc2a9..af91038 100644 --- a/core/lib/Drupal/Core/Pager/PagerManagerInterface.php +++ b/core/lib/Drupal/Core/Pager/PagerManagerInterface.php @@ -74,8 +74,8 @@ interface PagerManagerInterface { * // PagerManagerInterface::findPage(). This comes from a URL parameter, so * // here we are assuming that the URL parameter corresponds to an actual * // page of results that will exist within the set. - * $pager_parameters = \Drupal::service('pager.parameters'); - * $page = $pager_parameters->findPage(); + * $pager_manager = \Drupal::service('pager.manager'); + * $page = $pager_manager->findPage(); * $num_per_page = \Drupal::config('mymodule.settings')->get('num_per_page'); * $offset = $num_per_page * $page; * $result = mymodule_remote_search($keywords, $offset, $num_per_page); @@ -120,6 +120,24 @@ public function createPager($total, $limit, $element = 0); */ public function getPager($element = 0); + /** + * Returns the current page being requested for display within a pager. + * + * @param int $pager_id + * (optional) An integer to distinguish between multiple pagers on one page. + * + * @return int + * The number of the current requested page, within the pager represented by + * $element. This is determined from the URL query parameter + * \Drupal::request()->query->get('page'), or 0 by default. Note that this + * number may differ from the actual page being displayed. For example, if a + * search for "example text" brings up three pages of results, but a user + * visits search/node/example+text?page=10, this function will return 10, + * even though the default pager implementation adjusts for this and still + * displays the third page of search results at that URL. + */ + public function findPage($pager_id = 0): int; + /** * Gets the URL query parameter array of a pager link. * diff --git a/core/tests/Drupal/KernelTests/Core/Pager/PagerManagerTest.php b/core/tests/Drupal/KernelTests/Core/Pager/PagerManagerTest.php index 2dd48d6..3f14421 100644 --- a/core/tests/Drupal/KernelTests/Core/Pager/PagerManagerTest.php +++ b/core/tests/Drupal/KernelTests/Core/Pager/PagerManagerTest.php @@ -38,4 +38,19 @@ public function testGetUpdatedParameters() { $this->assertEquals(",,$index", $query['page']); } + /** + * @covers ::findPage + */ + public function testFindPage() { + $request = Request::create('http://example.com', 'GET', ['page' => '0,10']); + + /* @var $request_stack \Symfony\Component\HttpFoundation\RequestStack */ + $request_stack = $this->container->get('request_stack'); + $request_stack->push($request); + + $pager_manager = $this->container->get('pager.manager'); + + $this->assertEquals(10, $pager_manager->findPage(1)); + } + }