You are here

class PagerManager in Drupal 8

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Pager/PagerManager.php \Drupal\Core\Pager\PagerManager
  2. 10 core/lib/Drupal/Core/Pager/PagerManager.php \Drupal\Core\Pager\PagerManager

Provides a manager for pagers.

Pagers are cached, and can be retrieved when rendering.

Hierarchy

Expanded class hierarchy of PagerManager

1 string reference to 'PagerManager'
core.services.yml in core/core.services.yml
core/core.services.yml
1 service uses PagerManager
pager.manager in core/core.services.yml
Drupal\Core\Pager\PagerManager

File

core/lib/Drupal/Core/Pager/PagerManager.php, line 13

Namespace

Drupal\Core\Pager
View source
class PagerManager implements PagerManagerInterface {
  use DependencySerializationTrait;

  /**
   * The pager parameters.
   *
   * @var \Drupal\Core\Pager\PagerParametersInterface
   */
  protected $pagerParams;

  /**
   * An associative array of pagers.
   *
   * Implemented as an array consisting of:
   *   - key: the element id integer.
   *   - value: a \Drupal\Core\Pager\Pager.
   *
   * @var array
   */
  protected $pagers;

  /**
   * Construct a PagerManager object.
   *
   * @param \Drupal\Core\Pager\PagerParametersInterface $pager_params
   *   The pager parameters.
   */
  public function __construct(PagerParametersInterface $pager_params) {
    $this->pagerParams = $pager_params;
  }

  /**
   * {@inheritdoc}
   */
  public function createPager($total, $limit, $element = 0) {
    $currentPage = $this->pagerParams
      ->findPage($element);
    $pager = new Pager($total, $limit, $currentPage);
    $this
      ->setPager($pager, $element);
    return $pager;
  }

  /**
   * {@inheritdoc}
   */
  public function getPager($element = 0) {
    return isset($this->pagers[$element]) ? $this->pagers[$element] : NULL;
  }

  /**
   * {@inheritdoc}
   */
  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.
    $element_pages = [];
    $max = $this
      ->getMaxPagerElementId();
    for ($i = 0; $i <= $max; $i++) {
      $currentPage = ($pager = $this
        ->getPager($i)) ? $pager
        ->getCurrentPage() : NULL;
      $element_pages[] = $i == $element ? $index : $currentPage;
    }
    $query['page'] = 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 this function take precedence.
    if ($current_query = $this->pagerParams
      ->getQueryParameters()) {
      $query = array_merge($current_query, $query);
    }
    return $query;
  }

  /**
   * Gets the extent of the pager page element IDs.
   *
   * @return int
   *   The maximum element ID available, -1 if there are no elements.
   */
  protected function getMaxPagerElementId() {
    return empty($this->pagers) ? -1 : max(array_keys($this->pagers));
  }

  /**
   * Saves a pager to the static cache.
   *
   * @param \Drupal\Core\Pager\Pager $pager
   *   The pager.
   * @param int $element
   *   The pager index.
   */
  protected function setPager(Pager $pager, $element = 0) {
    $this->pagers[$element] = $pager;
    $this
      ->updateGlobals();
  }

  /**
   * Updates global variables with a pager data for backwards compatibility.
   */
  protected function updateGlobals() {
    $pager_total_items = [];
    $pager_total = [];
    $pager_page_array = [];
    $pager_limits = [];

    /** @var $pager \Drupal\Core\Pager\Pager */
    foreach ($this->pagers as $pager_id => $pager) {
      $pager_total_items[$pager_id] = $pager
        ->getTotalItems();
      $pager_total[$pager_id] = $pager
        ->getTotalPages();
      $pager_page_array[$pager_id] = $pager
        ->getCurrentPage();
      $pager_limits[$pager_id] = $pager
        ->getLimit();
    }
    $GLOBALS['pager_total_items'] = new DeprecatedArray($pager_total_items, 'Global variable $pager_total_items 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');
    $GLOBALS['pager_total'] = new DeprecatedArray($pager_total, 'Global variable $pager_total 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');
    $GLOBALS['pager_page_array'] = new DeprecatedArray($pager_page_array, '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');
    $GLOBALS['pager_limits'] = new DeprecatedArray($pager_limits, 'Global variable $pager_limits 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');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
PagerManager::$pagerParams protected property The pager parameters.
PagerManager::$pagers protected property An associative array of pagers.
PagerManager::createPager public function Initializes a pager. Overrides PagerManagerInterface::createPager
PagerManager::getMaxPagerElementId protected function Gets the extent of the pager page element IDs.
PagerManager::getPager public function Gets a pager from the static cache. Overrides PagerManagerInterface::getPager
PagerManager::getUpdatedParameters public function Gets the URL query parameter array of a pager link. Overrides PagerManagerInterface::getUpdatedParameters
PagerManager::setPager protected function Saves a pager to the static cache.
PagerManager::updateGlobals protected function Updates global variables with a pager data for backwards compatibility.
PagerManager::__construct public function Construct a PagerManager object.