You are here

function template_preprocess_views_mini_pager in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/views/views.theme.inc \template_preprocess_views_mini_pager()
  2. 9 core/modules/views/views.theme.inc \template_preprocess_views_mini_pager()

Prepares variables for views mini-pager templates.

Default template: views-mini-pager.html.twig.

Parameters

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.

File

core/modules/views/views.theme.inc, line 1052
Preprocessors and helper functions to make theming easier.

Code

function template_preprocess_views_mini_pager(&$variables) {

  /** @var \Drupal\Core\Pager\PagerManagerInterface $pager_manager */
  $pager_manager = \Drupal::service('pager.manager');
  $tags =& $variables['tags'];
  $element = $variables['element'];
  $parameters = $variables['parameters'];
  $pager = $pager_manager
    ->getPager($element);
  if (!$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();
  }
  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();
  }

  // This is based on the entire current query string. We need to ensure
  // cacheability is affected accordingly.
  $variables['#cache']['contexts'][] = 'url.query_args';
  $variables['heading_id'] = Html::getUniqueId('pagination-heading');
}