elasticsearch_search_api-1.0.x-dev/modules/esa_pager/esa_pager.module
modules/esa_pager/esa_pager.module
<?php
/**
* @file
* Contains esa_pager.module.
*/
use Drupal\Core\Template\Attribute;
use Drupal\Core\Url;
/**
* Implements hook_theme().
*/
function esa_pager_theme() {
return [
'esa_pager' => [
'template' => 'esa_pager',
'variables' => [
'tags' => [],
'element' => 0,
'parameters' => [],
'total_items' => 0,
'items_per_page' => 0,
'route_name' => '',
'route_params' => [],
],
],
];
}
/**
* Prepares variables for esa pager templates.
*
* Based on views mini pager and pager.inc.
*
* @param array $variables
* An associative array containing:
* - tags: Provides link text for the next/previous links.
* - element: The pager's id.
* - parameters: Any extra GET parameters that should be retained, such as
* exposed input.
*/
function esa_pager_preprocess_esa_pager(array &$variables) {
global $pager_page_array, $pager_total;
/** @var \Drupal\Core\Pager\PagerManagerInterface $pager_manager */
$pager_manager = \Drupal::service('pager.manager');
if (empty($pager_page_array)) {
$page = \Drupal::request()->request->getInt('page', 0);
$pager_page_array = [$variables['element'] => $page];
}
$total_items = $variables['total_items'];
$items_per_page = $variables['items_per_page'];
$total_pages = ceil($total_items / $items_per_page);
if (empty($pager_total)) {
$pager_total = [$variables['element'] => $total_pages];
}
$tags = &$variables['tags'];
$element = $variables['element'];
$parameters = $variables['parameters'];
$route_name = $variables['route_name'];
$route_params = $variables['route_params'] ?? [];
$pager = $pager_manager->getPager($element);
// Nothing to do if there is no pager.
if (!isset($pager)) {
return;
}
$current = $pager->getCurrentPage();
$total = $pager->getTotalPages();
// Current is the page we are currently paged to.
$variables['items']['current'] = $current + 1;
if ($total > 1 && $current > 0) {
$options = [
'query' => $pager_manager->getUpdatedParameters($parameters, $element, $current - 1),
];
$variables['items']['previous']['href'] = Url::fromRoute('<current>', [], $options)->toString();
if (isset($tags[1])) {
$variables['items']['previous']['text'] = $tags[1];
}
$variables['items']['previous']['attributes'] = new Attribute();
$variables['items']['previous']['attributes'] = ['data-page' => $current - 1];
}
if ($current < ($total - 1)) {
$options = [
'query' => $pager_manager->getUpdatedParameters($parameters, $element, $current + 1),
];
$variables['items']['next']['href'] = Url::fromRoute('<current>', [], $options)->toString();
if (isset($tags[3])) {
$variables['items']['next']['text'] = $tags[3];
}
$variables['items']['next']['attributes'] = new Attribute();
$variables['items']['next']['attributes'] = ['data-page' => $current + 1];
}
// Calculate various markers within this pager piece:
// Current is the page we are currently paged to.
$pager_current = $current + 1;
// First is the first page listed by this pager piece (re quantity).
$pager_first = 1;
// Last is the last page listed by this pager piece (re quantity).
$pager_last = $total_pages;
// Max is the maximum page number.
$pager_max = $pager_total[$element];
$i = $pager_first;
if ($pager_last > $pager_max) {
// Adjust "center" if at end of query.
$i = $i + ($pager_max - $pager_last);
$pager_last = $pager_max;
}
if ($i <= 0) {
// Adjust "center" if at start of query.
$pager_last = $pager_last + (1 - $i);
$i = 1;
}
$items['pages'] = [];
if ($i != $pager_max) {
// Add an ellipsis if there are further previous pages.
if ($i > 1) {
$variables['ellipses']['previous'] = TRUE;
}
// Now generate the actual pager piece.
$ellipsis_first_added = FALSE;
$ellipsis_second_added = FALSE;
if ($pager_current < 3) {
$ellipsis_first_added = TRUE;
}
if ($pager_current > $pager_max - 3) {
$ellipsis_second_added = TRUE;
}
$current_plus = ($pager_current == $pager_first) ? $pager_current + 2 : $pager_current + 1;
$current_minus = ($pager_current == $pager_last) ? $pager_current - 2 : $pager_current - 1;
for (; $i <= $pager_last && $i <= $pager_max; $i++) {
if ($i == $pager_current) {
$variables['current'] = $i;
}
if ($i == $pager_first || $i == $pager_last || ($i >= $current_minus && $i <= $current_plus)) {
$options = [
'query' => $pager_manager->getUpdatedParameters($parameters, $element, $i - 1),
];
$items['pages'][$i]['href'] = Url::fromRoute($route_name, $route_params, $options)->toString();
$items['pages'][$i]['attributes'] = ['data-page' => $i - 1];
}
else {
if (!$ellipsis_first_added && $i < $pager_current) {
$ellipsis_first_added = TRUE;
$items['pages'][$i] = 'ellipsis';
}
if (!$ellipsis_second_added && $i > $pager_current) {
$ellipsis_second_added = TRUE;
$items['pages'][$i] = 'ellipsis';
}
}
}
// Add an ellipsis if there are further next pages.
if ($i < $pager_max + 1) {
$variables['ellipses']['next'] = TRUE;
}
}
$variables['items']['pages'] = $items['pages'];
// This is based on the entire current query string. We need to ensure
// cacheability is affected accordingly.
$variables['#cache']['contexts'][] = 'url.query_args';
$variables['#attached']['library'] = ['esa_pager/pager'];
}
