diff --git a/core/core.services.yml b/core/core.services.yml index 38c5e97..4baeafa 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -41,7 +41,9 @@ parameters: exposedHeaders: false maxAge: false supportsCredentials: false - pager_class: Drupal\Core\Pager\Pager + pager.config: + class: Drupal\Core\Pager\Pager + querystring_parameter: 'page' services: # Simple cache contexts, directly derived from the request context. cache_context.ip: @@ -1642,4 +1644,4 @@ services: - { name: event_subscriber } pager.factory: class: Drupal\Core\Pager\PagerFactory - arguments: ['%pager_class%', '@service_container', '@request_stack'] + arguments: ['%pager.config%', '@service_container', '@request_stack'] diff --git a/core/includes/pager.inc b/core/includes/pager.inc index 98d277a..26131ed 100644 --- a/core/includes/pager.inc +++ b/core/includes/pager.inc @@ -147,7 +147,9 @@ function pager_default_initialize($total, $limit, $element = 0) { * instead. */ function pager_get_query_parameters() { - return \Drupal::service('pager.factory')->getCurrentRequestQueryParameters(['page']); + $pager_factory = \Drupal::service('pager.factory'); + $pager_parameter = $pager_factory->getQueryStringParameter(); + return $pager_factory->getCurrentRequestQueryParameters([$pager_parameter]); } /** diff --git a/core/lib/Drupal/Core/Pager/Pager.php b/core/lib/Drupal/Core/Pager/Pager.php index 5116ac7..ba71a5d 100644 --- a/core/lib/Drupal/Core/Pager/Pager.php +++ b/core/lib/Drupal/Core/Pager/Pager.php @@ -199,7 +199,7 @@ public function init($total, $limit) { * The page to which the pager is currently positioned to. */ protected function getCurrentPageFromRequest() { - $page_query = $this->factory->getCurrentRequestQueryParameter('page'); + $page_query = $this->factory->getCurrentRequestQueryParameter($this->factory->getQueryStringParameter()); $page_array = explode(',', $page_query); if (!isset($page_array[$this->element])) { $page_array[$this->element] = 0; @@ -284,7 +284,7 @@ public function getLinkQueryParameters($page) { // Get all defined pagers. $pagers = $this->factory->all(); - // Build the 'page' query parameter. This is built based on the current + // Build the pager query parameter. This is built based on the current // page of each pager element (or NULL if the pager is not set), with the // exception of the requested page index for the current element. $query = $this->getInputQueryParameters(); @@ -293,7 +293,8 @@ public function getLinkQueryParameters($page) { for ($i = 0; $i <= $max_element; $i++) { $element_pages[] = ($i == $this->getElement()) ? $page : (isset($pagers[$i]) ? $pagers[$i]->getCurrentPage() : NULL); } - $query['page'] = implode(',', $element_pages); + $parameter = $this->factory->getQueryStringParameter(); + $query[$parameter] = implode(',', $element_pages); // Merge the query parameters passed to this function with the parameters // from the current request. In case of collision, the parameters passed into diff --git a/core/lib/Drupal/Core/Pager/PagerFactory.php b/core/lib/Drupal/Core/Pager/PagerFactory.php index 5aac30d..c96795a 100644 --- a/core/lib/Drupal/Core/Pager/PagerFactory.php +++ b/core/lib/Drupal/Core/Pager/PagerFactory.php @@ -19,6 +19,16 @@ class PagerFactory implements PagerFactoryInterface { protected $pagerClass; /** + * The string to be used in URL querystring to identify the pager. + * + * Typically, 'page' so that the URL query will look like this: + * '?page=x,y,z'. + * + * @var string + */ + protected $pagerQueryStringParameter; + + /** * The array of pager objects, indexed by their element. * * @var \Drupal\Core\Pager\PagerInterface[] @@ -42,13 +52,17 @@ class PagerFactory implements PagerFactoryInterface { /** * Constructs a new PagerFactory object. * - * @param string $pager_class - * The class to be used to instantiate PagerInterface objects. + * @param array $pager_config + * An array with the following keys: + * 'class' - The class to be used to instantiate PagerInterface objects. + * 'querystring_parameter' - The string to be used in URL querystring to + * identify the pager. Typically, 'page'. * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack * The request stack. */ - public function __construct($pager_class, ContainerInterface $container, RequestStack $request_stack) { - $this->pagerClass = $pager_class; + public function __construct(array $pager_config, ContainerInterface $container, RequestStack $request_stack) { + $this->pagerClass = $pager_config['class']; + $this->pagerQueryStringParameter = $pager_config['querystring_parameter']; $this->container = $container; $this->requestStack = $request_stack; } @@ -90,6 +104,13 @@ public function all() { } /** + * {@inheritdoc} + */ + public function getQueryStringParameter() { + return $this->pagerQueryStringParameter ?: 'page'; + } + + /** * Returns the current request query parameter bag. * * @return \Symfony\Component\HttpFoundation\ParameterBag @@ -117,7 +138,8 @@ public function getCurrentRequestQueryParameters(array $filter = []) { } static $query, $filtered; if (!isset($query) || $filter !== $filtered) { - $query = UrlHelper::filterQueryParameters($this->getCurrentRequestQuery()->all(), ['page']); + $parameter = $this->getQueryStringParameter(); + $query = UrlHelper::filterQueryParameters($this->getCurrentRequestQuery()->all(), [$parameter]); $filtered = $filter; } return $query; diff --git a/core/lib/Drupal/Core/Pager/PagerFactoryInterface.php b/core/lib/Drupal/Core/Pager/PagerFactoryInterface.php index fc05731..54b4844 100644 --- a/core/lib/Drupal/Core/Pager/PagerFactoryInterface.php +++ b/core/lib/Drupal/Core/Pager/PagerFactoryInterface.php @@ -37,6 +37,17 @@ public function getFirstAvailableElement(); public function all(); /** + * Returns the string to be used in URL querystring to identify the pager. + * + * Typically, 'page' so that the URL query will look like this: + * '?page=x,y,z'. + * + * @return string + * The string to be used in URL querystring to identify the pager. + */ + public function getQueryStringParameter(); + + /** * Returns a query string parameter from the current request. * * @param string $parameter diff --git a/core/tests/Drupal/Tests/Core/Pager/PagerFactoryTest.php b/core/tests/Drupal/Tests/Core/Pager/PagerFactoryTest.php index 20f7aad..176419f 100644 --- a/core/tests/Drupal/Tests/Core/Pager/PagerFactoryTest.php +++ b/core/tests/Drupal/Tests/Core/Pager/PagerFactoryTest.php @@ -48,7 +48,7 @@ protected function setUp() { $this->pagerFactory = $this ->getMockBuilder(PagerFactory::class) ->setMethods(['getCurrentRequestQuery']) - ->setConstructorArgs([Pager::class, $container, $request_stack]) + ->setConstructorArgs([['class' => Pager::class, 'querystring_parameter' => 'page'], $container, $request_stack]) ->getMock(); $container->set('pager.factory', $this->pagerFactory); @@ -106,8 +106,16 @@ public function testAll() { // Get pager 6. $pager = $this->pagerFactory->get(6); $this->assertSame(6, $pager->getElement()); - // We should get 2 pagers. + // We should get two pagers for elements 4 and 6. $this->assertSame(2, count($this->pagerFactory->all())); + $this->assertSame([4, 6], array_keys($this->pagerFactory->all())); + } + + /** + * @covers ::getQueryStringParameter + */ + public function testGetQueryStringParameter() { + $this->assertSame('page', $this->pagerFactory->getQueryStringParameter()); } /**