You are here

function brainstorm_theme_preprocess_pager in Brainstorm profile 8

Implements hook_preprocess_HOOK().

File

theme/brainstorm_theme/brainstorm_theme.theme, line 97
Process theme data.

Code

function brainstorm_theme_preprocess_pager(&$variables) {
  $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'] : [];
  global $pager_page_array, $pager_total;

  // Nothing to do if there is only one page.
  if ($pager_total[$element] <= 1) {
    return;
  }
  $tags = $variables['pager']['#tags'];

  // Calculate various markers within this pager piece:
  // Middle is used to "center" pages around the current page.
  $pager_middle = ceil($quantity / 2);

  // Current is the page we are currently paged to.
  $pager_current = $pager_page_array[$element] + 1;

  // First is the first page listed by this pager piece (re quantity).
  $pager_first = $pager_current - $pager_middle + 1;

  // Last is the last page listed by this pager piece (re quantity).
  $pager_last = $pager_current + $quantity - $pager_middle;

  // Max is the maximum page number.
  $pager_max = $pager_total[$element];

  // Prepare for generation loop.
  $count = $pager_first;
  if ($pager_last > $pager_max) {

    // Adjust "center" if at end of query.
    $count = $count + ($pager_max - $pager_last);
    $pager_last = $pager_max;
  }
  if ($count <= 0) {

    // Adjust "center" if at start of query.
    $pager_last = $pager_last + (1 - $count);
    $count = 1;
  }

  // Create the "first" and "previous" links if we are not on the first page.
  if ($pager_page_array[$element] > 0) {
    $items['first'] = array();
    $options = array(
      'query' => pager_query_add_page($parameters, $element, 0),
    );
    $items['first']['href'] = \Drupal::url($route_name, $route_parameters, $options);
    if (isset($tags[0])) {
      $items['first']['text'] = $tags[0];
    }
  }
  if ($count != $pager_max) {

    // Add an ellipsis if there are further previous pages.
    if ($count > 1) {
      $variables['ellipses']['previous'] = TRUE;
    }

    // Now generate the actual pager piece.
    for (; $count <= $pager_last && $count <= $pager_max; $count++) {
      $options = array(
        'query' => pager_query_add_page($parameters, $element, $count - 1),
      );
      $items['pages'][$count]['href'] = \Drupal::url($route_name, $route_parameters, $options);
      if ($count == $pager_current) {
        $variables['current'] = $count;
      }
    }

    // Add an ellipsis if there are further next pages.
    if ($count < $pager_max + 1) {
      $variables['ellipses']['next'] = TRUE;
    }
  }

  // Create the "next" and "last" links if we are not on the last page.
  if ($pager_page_array[$element] < $pager_max - 1) {
    $items['last'] = array();
    $options = array(
      'query' => pager_query_add_page($parameters, $element, $pager_max - 1),
    );
    $items['last']['href'] = \Drupal::url($route_name, $route_parameters, $options);
    if (isset($tags[4])) {
      $items['last']['text'] = $tags[4];
    }
  }
  $variables['items'] = $items;

  // The rendered link needs to play well with any other query parameter used
  // on the page, like exposed filters, so for the cacheability all query
  // parameters matter.
  $variables['#cache']['contexts'][] = 'url.query_args';
}