function ViewsThemeHooks::preprocessViewsMiniPager

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.
  • pagination_heading_level: An optional heading level for the pager.
  • parameters: Any extra GET parameters that should be retained, such as exposed input.

File

core/modules/views/src/Hook/ViewsThemeHooks.php, line 1276

Class

ViewsThemeHooks
Hook implementations for views.

Namespace

Drupal\views\Hook

Code

public function preprocessViewsMiniPager(array &$variables) : void {
  if (empty($variables['pagination_heading_level'])) {
    $variables['pagination_heading_level'] = 'h4';
  }
  $tags =& $variables['tags'];
  $element = $variables['element'];
  $parameters = $variables['parameters'];
  $pager = $this->pagerManager
    ->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' => $this->pagerManager
        ->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' => $this->pagerManager
        ->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');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.