Commit a5789f3c authored by catch's avatar catch

Issue #3173636 by Pooja Ganjage, paulocs, anmolgoyal74, mondrake, alexpott,...

Issue #3173636 by Pooja Ganjage, paulocs, anmolgoyal74, mondrake, alexpott, catch: Implement a PagerManagerInterface::findPage() method
parent cf4a6098
......@@ -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}
*/
......
......@@ -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.
*
......
......@@ -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));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment