You are here

function template_preprocess_pagerer_base in Pagerer 8.2

Same name and namespace in other branches
  1. 8 pagerer.module \template_preprocess_pagerer_base()

Prepares variables for pagerer base templates.

Default template: pagerer-base.html.twig.

Parameters

array $variables: An associative array containing:

  • pager: A render element containing:

    • #id: (optional) The id attribute of the HTML element.
    • #style: The PagererStyle plugin id to be used to render the pager.
    • #element: (optional) An integer to distinguish between multiple pagers on one page.
    • #parameters: (optional) An associative array of query string parameters to append to the pager links.
    • #config: (optional) An associative array of configuration elements passed on to the styling plugin.
    • #embedded: (optional) Used internally to indicate a pager element contained within another pager for the scrollpane style.
    • #state: (optional) drupalSettings to be attached to the HTML page.

File

./pagerer.module, line 159
Pagerer.

Code

function template_preprocess_pagerer_base(array &$variables) {

  // Merge with default variables.
  $variables['pager'] = array_merge([
    '#id' => NULL,
    '#style' => 'standard',
    '#config' => [],
    '#embedded' => FALSE,
    '#state' => [],
  ], $variables['pager']);
  $pager = \Drupal::service('pager.manager')
    ->getPager($variables['pager']['#element']);

  // Nothing to do if there is no pager.
  if (!isset($pager)) {
    return;
  }

  // Move some variables from the pager element to root.
  $variables['style'] = $variables['pager']['#style'];
  $variables['embedded'] = $variables['pager']['#embedded'];
  $variables['id'] = $variables['pager']['#id'];

  // Add scrollpane specials.
  if ($variables['pager']['#embedded']) {

    // Add drupalSettings.
    $variables['#attached']['drupalSettings'] = [
      'pagerer' => [
        'state' => [
          $variables['pager']['#id'] => $variables['pager']['#state'],
        ],
      ],
    ];
  }

  // Let the style plugin do its own preprocessing.
  $pager
    ->setRouteName($variables['pager']['#route_name'])
    ->setRouteParameters(isset($variables['pager']['#route_parameters']) ? $variables['pager']['#route_parameters'] : []);
  \Drupal::service('pagerer.style.manager')
    ->createInstance($variables['pager']['#style'], $variables['pager']['#config'])
    ->setPager($pager)
    ->preprocess($variables);

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