From a5789f3cd7e5db971d322446ff3d1df719a70fed Mon Sep 17 00:00:00 2001
From: catch <catch@35733.no-reply.drupal.org>
Date: Mon, 16 Nov 2020 20:00:01 +0000
Subject: [PATCH] Issue #3173636 by Pooja Ganjage, paulocs, anmolgoyal74,
 mondrake, alexpott, catch: Implement a PagerManagerInterface::findPage()
 method

---
 core/lib/Drupal/Core/Pager/PagerManager.php   |  7 ++++++
 .../Core/Pager/PagerManagerInterface.php      | 22 +++++++++++++++++--
 .../Core/Pager/PagerManagerTest.php           | 15 +++++++++++++
 3 files changed, 42 insertions(+), 2 deletions(-)

diff --git a/core/lib/Drupal/Core/Pager/PagerManager.php b/core/lib/Drupal/Core/Pager/PagerManager.php
index 68827d2d6af3..5e29724dc1d9 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(int $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 27dc2a9715dc..94db9320ac33 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(int $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 2dd48d66362d..3f1442141cc8 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));
+  }
+
 }
-- 
GitLab