class Pagerer in Pagerer 8
Same name in this branch
- 8 src/Pagerer.php \Drupal\pagerer\Pagerer
- 8 src/Plugin/views/pager/Pagerer.php \Drupal\pagerer\Plugin\views\pager\Pagerer
Same name and namespace in other branches
- 8.2 src/Pagerer.php \Drupal\pagerer\Pagerer
Pagerer pager management class.
Hierarchy
- class \Drupal\pagerer\Pagerer implements ContainerInjectionInterface, PagererInterface
Expanded class hierarchy of Pagerer
3 files declare their use of Pagerer
- Multipane.php in src/
Plugin/ pagerer/ Multipane.php - PagererStyleBase.php in src/
Plugin/ pagerer/ PagererStyleBase.php - PagererStyleInterface.php in src/
Plugin/ PagererStyleInterface.php
3 string references to 'Pagerer'
File
- src/
Pagerer.php, line 13
Namespace
Drupal\pagererView source
class Pagerer implements PagererInterface, ContainerInjectionInterface {
/**
* The pager element.
*
* This is the index used by query extenders to identify the query
* to be paged, and reflected in the 'page=x,y,z' query parameter
* of the HTTP request.
*
* @var int
*/
protected $element;
/**
* The route name.
*
* @var string
*/
protected $routeName;
/**
* The route parameters.
*
* @var string[]
*/
protected $routeParameters = [];
/**
* The pager adaptive keys.
*
* @var string
*/
protected $adaptiveKeys;
/**
* The Pagerer factory.
*
* @var \Drupal\pagerer\PagererFactoryInterface
*/
protected $factory;
/**
* The request stack object.
*
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
protected $requestStack;
/**
* Constructs a new Pagerer pager object.
*
* @param \Drupal\pagerer\PagererFactoryInterface $pagerer_factory
* The Pagerer factory.
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
* The request stack.
*/
public function __construct(PagererFactoryInterface $pagerer_factory, RequestStack $request_stack) {
$this->factory = $pagerer_factory;
$this->requestStack = $request_stack;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, $element = NULL) {
$instance = new static($container
->get('pagerer.factory'), $container
->get('request_stack'));
// Set the pager element.
$instance->element = $element;
// Set the pager adaptive keys if they exist in the query string.
if ($page_ak = $instance
->getCurrentRequest()->query
->get('page_ak')) {
// A 'page_ak' query parameter exists in the calling URL.
$adaptive_keys = explode(',', $page_ak);
if (isset($adaptive_keys[$element])) {
$instance
->setAdaptiveKeys($adaptive_keys[$element]);
}
}
return $instance;
}
/**
* {@inheritdoc}
*/
public function getRouteName() {
return $this->routeName;
}
/**
* {@inheritdoc}
*/
public function setRouteName($route_name) {
$this->routeName = $route_name;
return $this;
}
/**
* {@inheritdoc}
*/
public function getRouteParameters() {
return $this->routeParameters;
}
/**
* {@inheritdoc}
*/
public function setRouteParameters(array $route_parameters) {
$this->routeParameters = $route_parameters;
return $this;
}
/**
* {@inheritdoc}
*/
public function getElement() {
return $this->element;
}
/**
* Returns the current request object.
*
* @return \Symfony\Component\HttpFoundation\Request
* The request object.
*/
protected function getCurrentRequest() {
return $this->requestStack
->getCurrentRequest();
}
/**
* {@inheritdoc}
*/
public function init($total, $limit) {
pager_default_initialize($total, $limit, $this->element);
return $this;
}
/**
* {@inheritdoc}
*/
public function getCurrentPage() {
global $pager_page_array;
return isset($pager_page_array[$this->element]) ? $pager_page_array[$this->element] : NULL;
}
/**
* {@inheritdoc}
*/
public function getTotalPages() {
global $pager_total;
return isset($pager_total[$this->element]) ? $pager_total[$this->element] : 0;
}
/**
* {@inheritdoc}
*/
public function getLastPage() {
global $pager_total;
return isset($pager_total[$this->element]) ? $pager_total[$this->element] - 1 : -1;
}
/**
* {@inheritdoc}
*/
public function getTotalItems() {
global $pager_total_items;
return isset($pager_total_items[$this->element]) ? $pager_total_items[$this->element] : 0;
}
/**
* {@inheritdoc}
*/
public function getLimit() {
global $pager_limits;
return isset($pager_limits[$this->element]) ? $pager_limits[$this->element] : 0;
}
/**
* {@inheritdoc}
*/
public function getAdaptiveKeys() {
return $this->adaptiveKeys;
}
/**
* Sets the adaptive keys of this pager.
*
* Used by the Adaptive pager style.
*
* @param string $adaptive_keys
* The adaptive keys string, in the format 'L.R.X', where L is the
* adaptive lock to left page, R is the adaptive lock to right page,
* and X is the adaptive center lock for calculation of neighborhood.
*
* @return \Drupal\pagerer\Pagerer
* The Pagerer pager object.
*/
protected function setAdaptiveKeys($adaptive_keys) {
$this->adaptiveKeys = $adaptive_keys;
return $this;
}
/**
* {@inheritdoc}
*/
public function getQueryParameters(array $parameters, $page, $adaptive_keys = NULL) {
// Build the 'page' and 'page_ak' query parameter elements.
// 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.
$page_el = [];
$page_ak = [];
foreach ($this->factory
->all() as $i => $p) {
$page_el[$i] = $i == $this
->getElement() ? $page : $p
->getCurrentPage();
$page_ak[$i] = $i == $this
->getElement() ? $adaptive_keys : $p
->getAdaptiveKeys();
}
// Build the 'page' and 'page_ak' fragments, removing unneeded trailing
// keys.
while (end($page_el) === NULL) {
array_pop($page_el);
}
$parameters['page'] = implode(',', $page_el);
while (end($page_ak) === NULL) {
array_pop($page_ak);
}
$parameters['page_ak'] = implode(',', $page_ak);
// Merge the updated pager query parameters, with any parameters coming
// from the current request. In case of collision, current parameters
// take precedence over the request ones.
if ($current_request_query = $this
->getCurrentRequest()->query
->all()) {
$parameters = array_merge($current_request_query, $parameters);
}
// Explicitly remove the segments if not relevant.
if (empty($page_el)) {
unset($parameters['page']);
}
if (empty($page_ak)) {
unset($parameters['page_ak']);
}
return $parameters;
}
/**
* {@inheritdoc}
*/
public function getHref(array $parameters, $page, $adaptive_keys = NULL, $set_query = TRUE) {
$options = $set_query ? [
'query' => $this
->getQueryParameters($parameters, $page, $adaptive_keys),
] : [];
return Url::fromRoute($this
->getRouteName(), $this
->getRouteParameters(), $options);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
Pagerer:: |
protected | property | The pager adaptive keys. | |
Pagerer:: |
protected | property | The pager element. | |
Pagerer:: |
protected | property | The Pagerer factory. | |
Pagerer:: |
protected | property | The request stack object. | |
Pagerer:: |
protected | property | The route name. | |
Pagerer:: |
protected | property | The route parameters. | |
Pagerer:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
|
Pagerer:: |
public | function |
Gets the adaptive keys of this pager. Overrides PagererInterface:: |
|
Pagerer:: |
public | function |
Gets the current page. Overrides PagererInterface:: |
|
Pagerer:: |
protected | function | Returns the current request object. | |
Pagerer:: |
public | function |
Gets the pager element. Overrides PagererInterface:: |
|
Pagerer:: |
public | function |
Gets a pager link. Overrides PagererInterface:: |
|
Pagerer:: |
public | function |
Gets last page in the pager (zero-index). Overrides PagererInterface:: |
|
Pagerer:: |
public | function |
Gets the items per page. Overrides PagererInterface:: |
|
Pagerer:: |
public | function |
Gets the query parameters array of a pager link. Overrides PagererInterface:: |
|
Pagerer:: |
public | function |
Gets the route name for this pager. Overrides PagererInterface:: |
|
Pagerer:: |
public | function |
Gets the route parameters for this pager. Overrides PagererInterface:: |
|
Pagerer:: |
public | function |
Gets total items in the pager. Overrides PagererInterface:: |
|
Pagerer:: |
public | function |
Gets total pages in the pager. Overrides PagererInterface:: |
|
Pagerer:: |
public | function |
Initializes the pager. Overrides PagererInterface:: |
|
Pagerer:: |
protected | function | Sets the adaptive keys of this pager. | |
Pagerer:: |
public | function |
Sets the route name for this pager. Overrides PagererInterface:: |
|
Pagerer:: |
public | function |
Sets the route parameters for this pager. Overrides PagererInterface:: |
|
Pagerer:: |
public | function | Constructs a new Pagerer pager object. |