You are here

cleanpager.module in Clean Pagination 8

Same filename and directory in other branches
  1. 5 cleanpager.module
  2. 6 cleanpager.module
  3. 7 cleanpager.module

Fto Clean Pager module file.

File

cleanpager.module
View source
<?php

/**
 * @file
 * Fto Clean Pager module file.
 */
define('VIEWS_AJAX_PAGE', '/views/ajax');
use Drupal\Core\Template\Attribute;

/**
 * Implements template_preprocess_pager().
 */
function cleanpager_preprocess_pager(&$variables) {
  global $pager_page_array, $pager_total;
  $element = $variables['pager']['#element'];
  $parameters = $variables['pager']['#parameters'];
  $quantity = $variables['pager']['#quantity'];
  $route_name = $variables['pager']['#route_name'];
  $route_parameters = isset($variables['pager']['#route_parameters']) ? $variables['pager']['#route_parameters'] : [];
  if ($pager_total[$element] <= 1) {
    return;
  }
  $tags = $variables['pager']['#tags'];
  $pager_middle = ceil($quantity / 2);
  $pager_current = $pager_page_array[$element] + 1;
  $pager_first = $pager_current - $pager_middle + 1;
  $pager_last = $pager_current + $quantity - $pager_middle;
  $pager_max = $pager_total[$element];
  $i = $pager_first;
  if ($pager_last > $pager_max) {
    $i = $i + ($pager_max - $pager_last);
    $pager_last = $pager_max;
  }
  if ($i <= 0) {
    $pager_last = $pager_last + (1 - $i);
    $i = 1;
  }
  if ($pager_page_array[$element] > 0) {
    $items['first'] = array();
    $options = array(
      'query' => pager_query_add_page($parameters, $element, 0),
    );
    $items['first']['href'] = _cleanpager_url($route_name, $route_parameters, $options);
    if (isset($tags[0])) {
      $items['first']['text'] = $tags[0];
    }
    $items['previous'] = array();
    $options = array(
      'query' => pager_query_add_page($parameters, $element, $pager_page_array[$element] - 1),
    );
    $items['previous']['href'] = _cleanpager_url($route_name, $route_parameters, $options);
    if (isset($tags[1])) {
      $items['previous']['text'] = $tags[1];
    }
  }
  if ($i != $pager_max) {
    if ($i > 1) {
      $variables['ellipses']['previous'] = TRUE;
    }
    for (; $i <= $pager_last && $i <= $pager_max; $i++) {
      $options = array(
        'query' => pager_query_add_page($parameters, $element, $i - 1),
      );
      $items['pages'][$i]['href'] = _cleanpager_url($route_name, $route_parameters, $options);
      if ($i == $pager_current) {
        $variables['current'] = $i;
      }
    }
    if ($i < $pager_max + 1) {
      $variables['ellipses']['next'] = TRUE;
    }
  }
  if ($pager_page_array[$element] < $pager_max - 1) {
    $items['next'] = array();
    $options = array(
      'query' => pager_query_add_page($parameters, $element, $pager_page_array[$element] + 1),
    );
    $items['next']['href'] = _cleanpager_url($route_name, $route_parameters, $options);
    if (isset($tags[3])) {
      $items['next']['text'] = $tags[3];
    }
    $items['last'] = array();
    $options = array(
      'query' => pager_query_add_page($parameters, $element, $pager_max - 1),
    );
    $items['last']['href'] = _cleanpager_url($route_name, $route_parameters, $options);
    if (isset($tags[4])) {
      $items['last']['text'] = $tags[4];
    }
  }
  $variables['items'] = $items;
  $variables['#cache']['contexts'][] = 'url.query_args';
}

/**
 * Implements template_preprocess_views_mini_pager().
 */
function cleanpager_preprocess_views_mini_pager(&$variables) {
  global $pager_page_array, $pager_total;
  $tags =& $variables['tags'];
  $element = $variables['element'];
  $parameters = $variables['parameters'];
  $variables['items']['current'] = $pager_page_array[$element] + 1;
  if ($pager_total[$element] > 1 && $pager_page_array[$element] > 0) {
    $options = array(
      'query' => pager_query_add_page($parameters, $element, $pager_page_array[$element] - 1),
    );
    $variables['items']['previous']['href'] = _cleanpager_url('<none>', [], $options);
    if (isset($tags[1])) {
      $variables['items']['previous']['text'] = $tags[1];
    }
    $variables['items']['previous']['attributes'] = new Attribute();
  }
  if ($pager_page_array[$element] < $pager_total[$element] - 1) {
    $options = array(
      'query' => pager_query_add_page($parameters, $element, $pager_page_array[$element] + 1),
    );
    $variables['items']['next']['href'] = _cleanpager_url('<none>', [], $options);
    if (isset($tags[3])) {
      $variables['items']['next']['text'] = $tags[3];
    }
    $variables['items']['next']['attributes'] = new Attribute();
  }
  $variables['#cache']['contexts'][] = 'url.query_args';
}

/**
 * Create clean url for pager pager link.
 */
function _cleanpager_url($route_name, $route_parameters, $options) {
  global $_cleanpager_pagination;
  $url = '';
  $uri = \Drupal::request()
    ->getPathInfo();
  if ($_cleanpager_pagination) {
    if ($page_pos = strpos($uri, '/page/')) {
      $q = substr($uri, 0, $page_pos);
    }
    else {
      $q = $uri;
    }
    if (isset($options['query']['page'])) {
      if ($q === '/') {
        $q .= '';
      }
      $url = $q . '/page/' . $options['query']['page'];
      if (\Drupal::configFactory()
        ->get('cleanpager.settings')
        ->get('cleanpager_add_trailing')) {
        $url .= '/';
      }
      unset($options['query']['page']);
    }
  }
  unset($options['query']['q']);
  $url .= \Drupal::url($route_name, $route_parameters, $options);
  return $url;
}

Functions

Constants

Namesort descending Description
VIEWS_AJAX_PAGE @file Fto Clean Pager module file.