You are here

function cleanpager_preprocess_pager in Clean Pagination 8

Implements template_preprocess_pager().

File

./cleanpager.module, line 15
Fto Clean Pager module file.

Code

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';
}