diff --git a/core/modules/views/src/Plugin/views/pager/SqlBase.php b/core/modules/views/src/Plugin/views/pager/SqlBase.php index 4c8cc0def4..d23e408928 100644 --- a/core/modules/views/src/Plugin/views/pager/SqlBase.php +++ b/core/modules/views/src/Plugin/views/pager/SqlBase.php @@ -5,12 +5,63 @@ use Drupal\Core\Cache\Cache; use Drupal\Core\Cache\CacheableDependencyInterface; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Pager\PagerManagerInterface; use Drupal\Core\Pager\RequestPager; +use Drupal\Core\Plugin\ContainerFactoryPluginInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; +use Symfony\Component\HttpFoundation\RequestStack; /** * A common base class for sql based pager. */ -abstract class SqlBase extends PagerPluginBase implements CacheableDependencyInterface { +abstract class SqlBase extends PagerPluginBase implements CacheableDependencyInterface, ContainerFactoryPluginInterface { + + /** + * The pager manager. + * + * @var \Drupal\Core\Pager\PagerManagerInterface + */ + protected $pagerManager; + + /** + * The request stack. + * + * @var \Symfony\Component\HttpFoundation\RequestStack + */ + protected $requestStack; + + /** + * Constructs a SqlBase object. + * + * @param array $configuration + * A configuration array containing information about the plugin instance. + * @param string $plugin_id + * The plugin_id for the plugin instance. + * @param mixed $plugin_definition + * The plugin implementation definition. + * @param \Drupal\Core\Pager\PagerManagerInterface $pager_manager + * The pager manager. + * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack + * The request stack. + */ + public function __construct(array $configuration, $plugin_id, $plugin_definition, PagerManagerInterface $pager_manager, RequestStack $request_stack) { + parent::__construct($configuration, $plugin_id, $plugin_definition); + $this->pagerManager = $pager_manager; + $this->requestStack = $request_stack; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('pager.manager'), + $container->get('request_stack') + ); + } protected function defineOptions() { $options = parent::defineOptions(); @@ -251,8 +302,7 @@ public function setCurrentPage($number = NULL) { return; } - $request_stack = \Drupal::service('request_stack'); - $pager_request = new RequestPager($request_stack); + $pager_request = new RequestPager($this->requestStack); $this->current_page = max(0, intval($pager_request->findPage($this->options['id']))); } @@ -282,9 +332,7 @@ public function updatePageInfo() { // Don't set pager settings for items per page = 0. $items_per_page = $this->getItemsPerPage(); if (!empty($items_per_page)) { - /** @var \Drupal\Core\Pager\PagerManagerInterface $pager_manager */ - $pager_manager = \Drupal::service('pager.manager'); - $pager = $pager_manager->createPager($this->total_items, $this->options['items_per_page'], $this->options['id']); + $pager = $this->pagerManager->createPager($this->total_items, $this->options['items_per_page'], $this->options['id']); // See if the requested page was within range. if ($this->current_page >= $pager->getTotalItems()) { // Pages are numbered from 0 so if there are 10 pages, the last page is