diff --git a/core/includes/pager.inc b/core/includes/pager.inc index 07818e177b..c18507331a 100644 --- a/core/includes/pager.inc +++ b/core/includes/pager.inc @@ -239,7 +239,7 @@ function template_preprocess_pager(&$variables) { $items['first'] = []; $items['first']['attributes'] = new Attribute(); $options = [ - 'query' => $pager_manager->updateParameters($parameters, $element, 0), + 'query' => $pager_manager->getUpdatedParameters($parameters, $element, 0), ]; $items['first']['href'] = Url::fromRoute($route_name, $route_parameters, $options)->toString(); if (isset($tags[0])) { @@ -249,7 +249,7 @@ function template_preprocess_pager(&$variables) { $items['previous'] = []; $items['previous']['attributes'] = new Attribute(); $options = [ - 'query' => $pager_manager->updateParameters($parameters, $element, $current_page - 1), + 'query' => $pager_manager->getUpdatedParameters($parameters, $element, $current_page - 1), ]; $items['previous']['href'] = Url::fromRoute($route_name, $route_parameters, $options)->toString(); if (isset($tags[1])) { @@ -265,7 +265,7 @@ function template_preprocess_pager(&$variables) { // Now generate the actual pager piece. for (; $i <= $pager_last && $i <= $pager_max; $i++) { $options = [ - 'query' => $pager_manager->updateParameters($parameters, $element, $i - 1), + 'query' => $pager_manager->getUpdatedParameters($parameters, $element, $i - 1), ]; $items['pages'][$i]['href'] = Url::fromRoute($route_name, $route_parameters, $options)->toString(); $items['pages'][$i]['attributes'] = new Attribute(); @@ -284,7 +284,7 @@ function template_preprocess_pager(&$variables) { $items['next'] = []; $items['next']['attributes'] = new Attribute(); $options = [ - 'query' => $pager_manager->updateParameters($parameters, $element, $current_page + 1), + 'query' => $pager_manager->getUpdatedParameters($parameters, $element, $current_page + 1), ]; $items['next']['href'] = Url::fromRoute($route_name, $route_parameters, $options)->toString(); if (isset($tags[3])) { @@ -294,7 +294,7 @@ function template_preprocess_pager(&$variables) { $items['last'] = []; $items['last']['attributes'] = new Attribute(); $options = [ - 'query' => $pager_manager->updateParameters($parameters, $element, $pager_max - 1), + 'query' => $pager_manager->getUpdatedParameters($parameters, $element, $pager_max - 1), ]; $items['last']['href'] = Url::fromRoute($route_name, $route_parameters, $options)->toString(); if (isset($tags[4])) { @@ -338,11 +338,11 @@ function template_preprocess_pager(&$variables) { * \Drupal\Core\Pager\PagerManagerInterface->queryAddPage() instead. * * @see https://www.drupal.org/node/2779457 - * @see \Drupal\Core\Pager\PagerManagerInterface::updateParameters() + * @see \Drupal\Core\Pager\PagerManagerInterface::getUpdatedParameters() */ function pager_query_add_page(array $query, $element, $index) { @trigger_error(__FUNCTION__ . ' is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use \Drupal\Core\Pager\PagerManagerInterface->queryAddPage() instead. See https://www.drupal.org/node/2779457', E_USER_DEPRECATED); /* @var $pager_manager \Drupal\Core\Pager\PagerManagerInterface */ $pager_manager = \Drupal::service('pager.manager'); - return $pager_manager->updateParameters($query, $element, $index); + return $pager_manager->getUpdatedParameters($query, $element, $index); } diff --git a/core/lib/Drupal/Core/Cache/Context/PagersCacheContext.php b/core/lib/Drupal/Core/Cache/Context/PagersCacheContext.php index 47221c654e..ed462f24f1 100644 --- a/core/lib/Drupal/Core/Cache/Context/PagersCacheContext.php +++ b/core/lib/Drupal/Core/Cache/Context/PagersCacheContext.php @@ -3,6 +3,7 @@ namespace Drupal\Core\Cache\Context; use Drupal\Core\Cache\CacheableMetadata; +use Drupal\Core\DependencyInjection\DeprecatedServicePropertyTrait; use Drupal\Core\Pager\PagerParametersInterface; /** @@ -14,6 +15,13 @@ */ class PagersCacheContext implements CalculatedCacheContextInterface { + use DeprecatedServicePropertyTrait; + + /** + * {@inheritdoc} + */ + protected $deprecatedProperties = ['requestStack' => 'request_stack']; + /** * The pager parameters. * @@ -27,9 +35,9 @@ class PagersCacheContext implements CalculatedCacheContextInterface { * @param \Drupal\Core\Pager\PagerParametersInterface $pager_params * The pager parameters. */ - public function __construct(PagerParametersInterface $pager_params = NULL) { - if (!$pager_params) { - @trigger_error('Calling ' . __METHOD__ . ' without the $pager_params argument is deprecated in drupal:8.8.0 and is required in drupal:9.0.0. See https://www.drupal.org/node/2779457', E_USER_DEPRECATED); + public function __construct($pager_params) { + if (!($pager_params instanceof PagerParametersInterface)) { + @trigger_error('Calling ' . __METHOD__ . ' with a $pager_params argument that does not implement \Drupal\Core\Pager\PagerParametersInterface is deprecated in drupal:8.8.0 and is required in drupal:9.0.0. See https://www.drupal.org/node/2779457', E_USER_DEPRECATED); $pager_params = \Drupal::service('pager.parameters'); } $this->pagerParams = $pager_params; diff --git a/core/lib/Drupal/Core/Pager/Pager.php b/core/lib/Drupal/Core/Pager/Pager.php index 4e9982b1b1..dedeed2657 100644 --- a/core/lib/Drupal/Core/Pager/Pager.php +++ b/core/lib/Drupal/Core/Pager/Pager.php @@ -63,7 +63,7 @@ public function __construct($totalItems, $limit, $currentPage = 0) { * (optional) The current page. */ protected function setCurrentPage($currentPage = 0) { - $this->currentPage = max(0, min($currentPage, ((int) $this->getTotalPages()) - 1)); + $this->currentPage = max(0, min($currentPage, $this->getTotalPages() - 1)); } /** diff --git a/core/lib/Drupal/Core/Pager/PagerManager.php b/core/lib/Drupal/Core/Pager/PagerManager.php index eea7e5867a..b97b813d7d 100644 --- a/core/lib/Drupal/Core/Pager/PagerManager.php +++ b/core/lib/Drupal/Core/Pager/PagerManager.php @@ -8,7 +8,7 @@ /** * Provides a manager for pagers. * - * Pagers are statically cached, and can be retrieved when rendering. + * Pagers are cached, and can be retrieved when rendering. */ class PagerManager implements PagerManagerInterface { @@ -22,7 +22,7 @@ class PagerManager implements PagerManagerInterface { protected $pagerParams; /** - * A static cache of pagers. + * An associative array of pagers. * * Implemented as an array consisting of: * - key: the element id integer. @@ -40,7 +40,6 @@ class PagerManager implements PagerManagerInterface { */ public function __construct(PagerParametersInterface $pager_params) { $this->pagerParams = $pager_params; - $this->initializeGlobals(); } /** @@ -50,7 +49,6 @@ public function createPager($total, $limit, $element = 0) { $currentPage = $this->pagerParams->findPage($element); $pager = new Pager($total, $limit, $currentPage); $this->setPager($pager, $element); - $this->updateGlobals(); return $pager; } @@ -64,7 +62,7 @@ public function getPager($element = 0) { /** * {@inheritdoc} */ - public function updateParameters(array $query, $element, $index) { + public function getUpdatedParameters(array $query, $element, $index) { // Build the 'page' 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. @@ -108,15 +106,6 @@ protected function setPager(Pager $pager, $element = 0) { $this->updateGlobals(); } - /** - * Initialize global variables for backwards compatibility. - */ - protected function initializeGlobals() { - if (empty($GLOBALS['pager_page_array'])) { - $GLOBALS['pager_page_array'] = new DeprecatedArray($this->pagerParams->getPagerQuery(), 'Global variable $pager_page_array is deprecated in drupal:8.8.0 and is removed in drupal:9.0.0. Use \Drupal\Core\Pager\PagerManagerInterface instead. See https://www.drupal.org/node/2779457'); - } - } - /** * Updates global variables with a pager data for backwards compatibility. */ diff --git a/core/lib/Drupal/Core/Pager/PagerManagerInterface.php b/core/lib/Drupal/Core/Pager/PagerManagerInterface.php index 83bbac5082..77ead948cc 100644 --- a/core/lib/Drupal/Core/Pager/PagerManagerInterface.php +++ b/core/lib/Drupal/Core/Pager/PagerManagerInterface.php @@ -12,7 +12,7 @@ * * Since there can be multiple pagers per requested page, each one is * represented by an 'element' ID. This is an integer. It represents the index - * of the pager element within the 'page' query. The pager element is an + * of the pager element within the 'page' query. The value of the element is an * integer telling us the current page number for that pager. * * This class generally replaces the functions in core/includes/pager.inc. Those @@ -43,9 +43,9 @@ interface PagerManagerInterface { * @endcode * * However, if you are using a different method for generating the items to be - * paged through, then you should call this function in preparation. + * paged through, then you should call this service in preparation. * - * The following example shows how this function can be used in a controller + * The following example shows how this service can be used in a controller * that invokes an external datastore with an SQL-like syntax: * @code * // First find the total number of items and initialize the pager. @@ -76,17 +76,19 @@ interface PagerManagerInterface { * obtain this information). Here, we call PagerManagerInterface->findPage() * to calculate the desired offset before the search is invoked: * @code - * $pager_manager = \Drupal::service('pager.manager'); + * * // Perform the query, using the requested offset from * // 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. - * $page = $pager_manager->findPage(); + * $pager_parameters = \Drupal::service('pager.parameters'); + * $page = $pager_parameters->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); * * // Now that we have the total number of results, initialize the pager. + * $pager_manager = \Drupal::service('pager.manager'); * $pager_manager->createPager($result->total, $num_per_page); * * // Create a render array with the search results. @@ -148,6 +150,6 @@ public function getPager($element = 0); * @return array * The altered $query parameter array. */ - public function updateParameters(array $query, $element, $index); + public function getUpdatedParameters(array $query, $element, $index); } diff --git a/core/lib/Drupal/Core/Pager/PagerParametersInterface.php b/core/lib/Drupal/Core/Pager/PagerParametersInterface.php index 3665d85ac4..a88286e512 100644 --- a/core/lib/Drupal/Core/Pager/PagerParametersInterface.php +++ b/core/lib/Drupal/Core/Pager/PagerParametersInterface.php @@ -10,7 +10,7 @@ interface PagerParametersInterface { /** - * Get all request URL query parameters that are unrelated to paging. + * Gets all request URL query parameters that are unrelated to paging. * * @return array * A URL query parameter array that consists of all components of the @@ -37,7 +37,7 @@ public function getQueryParameters(); public function findPage($pager_id = 0); /** - * Get the request query parameter. + * Gets the request query parameter. * * @return int[] * Array of pagers. Keys are integers which are the element ID. Values are @@ -47,7 +47,7 @@ public function findPage($pager_id = 0); public function getPagerQuery(); /** - * Get the 'page' query parameter for the current request. + * Gets the 'page' query parameter for the current request. * * @return string * The 'page' query parameter for the current request. This is a diff --git a/core/lib/Drupal/Core/Render/Element/Pager.php b/core/lib/Drupal/Core/Render/Element/Pager.php index 637bd317fa..948fd7789f 100644 --- a/core/lib/Drupal/Core/Render/Element/Pager.php +++ b/core/lib/Drupal/Core/Render/Element/Pager.php @@ -6,7 +6,7 @@ * Provides a render element for a pager. * * The pager must be initialized with a call to - * \Drupal::service('pager.manager)->createPager() in order to render + * \Drupal\Core\Pager\PagerManagerInterface::createPager() in order to render * properly. When used with database queries, this is performed for you when you * extend a select query with \Drupal\Core\Database\Query\PagerSelectExtender. * @@ -69,8 +69,8 @@ public function getInfo() { public static function preRenderPager(array $pager) { // Note: the default pager theme process function // template_preprocess_pager() also calls - // \Drupal::service('pager.manager)->queryAddPage(), which maintains the - // existing query string. Therefore template_preprocess_pager() adds the + // \Drupal\Core\Pager\PagerManagerInterface::queryAddPage(), which maintains + // the existing query string. Therefore template_preprocess_pager() adds the // 'url.query_args' cache context, which causes the more specific cache // context below to be optimized away. In other themes, however, that may // not be the case. diff --git a/core/modules/views/src/Plugin/views/pager/SqlBase.php b/core/modules/views/src/Plugin/views/pager/SqlBase.php index 3ca9677815..29b3ba850f 100644 --- a/core/modules/views/src/Plugin/views/pager/SqlBase.php +++ b/core/modules/views/src/Plugin/views/pager/SqlBase.php @@ -9,7 +9,6 @@ use Drupal\Core\Pager\PagerParameters; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Symfony\Component\DependencyInjection\ContainerInterface; -use Symfony\Component\HttpFoundation\RequestStack; /** * A common base class for sql based pager. @@ -30,6 +29,13 @@ abstract class SqlBase extends PagerPluginBase implements CacheableDependencyInt */ protected $requestStack; + /** + * The pager parameters. + * + * @var \Drupal\Core\Pager\PagerParametersInterface + */ + protected $pagerParameters; + /** * Constructs a SqlBase object. * @@ -41,21 +47,21 @@ abstract class SqlBase extends PagerPluginBase implements CacheableDependencyInt * The plugin implementation definition. * @param \Drupal\Core\Pager\PagerManagerInterface $pager_manager * The pager manager. - * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack - * The request stack. + * @param \Drupal\Core\Pager\PagerParameters|null $pager_parameters + * The pager parameters. */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, PagerManagerInterface $pager_manager = NULL, RequestStack $request_stack = NULL) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, PagerManagerInterface $pager_manager = NULL, PagerParameters $pager_parameters = NULL) { parent::__construct($configuration, $plugin_id, $plugin_definition); if (!$pager_manager) { @trigger_error('Calling ' . __METHOD__ . ' without the $pager_manager argument is deprecated in drupal:8.8.0 and is required in drupal:9.0.0. See https://www.drupal.org/node/2779457', E_USER_DEPRECATED); $pager_manager = \Drupal::service('pager.manager'); } $this->pagerManager = $pager_manager; - if (!$request_stack) { - @trigger_error('Calling ' . __METHOD__ . ' without the $request_stack argument is deprecated in drupal:8.8.0 and is required in drupal:9.0.0. See https://www.drupal.org/node/2779457', E_USER_DEPRECATED); - $request_stack = \Drupal::service('request_stack'); + if (!$pager_parameters) { + @trigger_error('Calling ' . __METHOD__ . ' without the $pager_parameters argument is deprecated in drupal:8.8.0 and is required in drupal:9.0.0. See https://www.drupal.org/node/2779457', E_USER_DEPRECATED); + $pager_parameters = \Drupal::service('pager.parameters'); } - $this->requestStack = $request_stack; + $this->pagerParameters = $pager_parameters; } /** @@ -67,7 +73,7 @@ public static function create(ContainerInterface $container, array $configuratio $plugin_id, $plugin_definition, $container->get('pager.manager'), - $container->get('request_stack') + $container->get('pager.parameters') ); } @@ -310,8 +316,7 @@ public function setCurrentPage($number = NULL) { return; } - $pager_params = new PagerParameters($this->requestStack); - $this->current_page = max(0, $pager_params->findPage($this->options['id'])); + $this->current_page = max(0, $this->pagerParameters->findPage($this->options['id'])); } public function getPagerTotal() { diff --git a/core/modules/views/views.theme.inc b/core/modules/views/views.theme.inc index 6a4eab3272..509a146e03 100644 --- a/core/modules/views/views.theme.inc +++ b/core/modules/views/views.theme.inc @@ -1018,7 +1018,7 @@ function template_preprocess_views_mini_pager(&$variables) { if ($total > 1 && $current > 0) { $options = [ - 'query' => $pager_manager->updateParameters($parameters, $element, $current - 1), + 'query' => $pager_manager->getUpdatedParameters($parameters, $element, $current - 1), ]; $variables['items']['previous']['href'] = Url::fromRoute('', [], $options)->toString(); if (isset($tags[1])) { @@ -1029,7 +1029,7 @@ function template_preprocess_views_mini_pager(&$variables) { if ($current < ($total - 1)) { $options = [ - 'query' => $pager_manager->updateParameters($parameters, $element, $current + 1), + 'query' => $pager_manager->getUpdatedParameters($parameters, $element, $current + 1), ]; $variables['items']['next']['href'] = Url::fromRoute('', [], $options)->toString(); if (isset($tags[3])) { diff --git a/core/tests/Drupal/KernelTests/Core/Pager/PagerManagerTest.php b/core/tests/Drupal/KernelTests/Core/Pager/PagerManagerTest.php index d6c20a33bd..a03e759ad3 100644 --- a/core/tests/Drupal/KernelTests/Core/Pager/PagerManagerTest.php +++ b/core/tests/Drupal/KernelTests/Core/Pager/PagerManagerTest.php @@ -36,7 +36,7 @@ public function testDefaultInitializeGlobals() { } /** - * @covers ::updateParameters + * @covers ::getUpdatedParameters */ public function testQueryAddPage() { $element = 2; @@ -54,7 +54,7 @@ public function testQueryAddPage() { $pager_manager = $this->container->get('pager.manager'); $pager_manager->createPager(30, 10, $element); - $query = $pager_manager->updateParameters($request->query->all(), $element, $index); + $query = $pager_manager->getUpdatedParameters($request->query->all(), $element, $index); $this->assertArrayHasKey('other', $query); @@ -73,7 +73,7 @@ public function testGlobalsSafety() { /* @var $pager_manager \Drupal\Core\Pager\PagerManagerInterface */ $pager_manager = $this->container->get('pager.manager'); - $pager_manager->createPager(5, 1); + $pager_manager->createPager(30, 10); $pager_globals = [ 'pager_page_array', @@ -86,15 +86,10 @@ public function testGlobalsSafety() { $this->assertTrue(isset($GLOBALS[$pager_global])); } - // Update globals outside the PagerManager. - $GLOBALS['pager_page_array'][] = ['foo']; - $GLOBALS['pager_total_items'][] = ['bar']; - $GLOBALS['pager_total'][] = ['baz']; - $GLOBALS['pager_limits'][] = ['wiz']; - - // Trigger the check if globals have changed. - $pager_manager->createPager(5, 1); - + $this->assertEquals(0, $GLOBALS['pager_page_array'][0]); + $this->assertEquals(30, $GLOBALS['pager_total_items'][0]); + $this->assertEquals(3, $GLOBALS['pager_total'][0]); + $this->assertEquals(10, $GLOBALS['pager_limits'][0]); } } diff --git a/core/tests/Drupal/KernelTests/Core/Pager/RequestPagerTest.php b/core/tests/Drupal/KernelTests/Core/Pager/RequestPagerTest.php index 27dddcb17d..bb1bc9066b 100644 --- a/core/tests/Drupal/KernelTests/Core/Pager/RequestPagerTest.php +++ b/core/tests/Drupal/KernelTests/Core/Pager/RequestPagerTest.php @@ -43,6 +43,7 @@ public function testGetQueryParameters() { $pager_params = $this->container->get('pager.parameters'); $this->assertEquals($test_parameters, $pager_params->getQueryParameters()); + $this->assertEquals(0, $pager_params->findPage()); } }