You are here

pagerer.module in Pagerer 8.2

Same filename and directory in other branches
  1. 8 pagerer.module
  2. 7 pagerer.module

Pagerer.

A collection of pager styles to enhance Drupal standard pager.

File

pagerer.module
View source
<?php

/**
 * @file
 * Pagerer.
 *
 * A collection of pager styles to enhance Drupal standard pager.
 */
use Drupal\Core\Routing\RouteMatchInterface;

/**
 * Implements hook_help().
 */
function pagerer_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'entity.pagerer_preset.collection':
      return '<p>' . t("Pagerer allows to pre-define multiple pager configurations.") . '</p>';
    case 'entity.pagerer_preset.edit_form':
      return '<p>' . t("Select a Pagerer style for each of the left, center and right panes. Each pane can be further configured clicking the appropriate action button.") . '</p>';
    case 'pagerer.url_settings':
      $output = '<p>' . t("Pagerer allows replacing the standard <kbd>page</kbd> querystring parameter in URLs with its own version. That can also allow starting the page count from 1 rather than from 0 in URLs.") . '</p>';
      $output .= '<dt>' . t('<b>Example</b>') . '</dt>';
      $output .= '<dd>' . t("Select the <em>URL querystring</em> checkbox, and <em>One-based</em> from <em>Page index base</em>. This will turn a <kbd>?page=0</kbd> pager URL into <kbd>?pg=1</kbd>.") . '</dd>';
      return $output;
  }
}

/**
 * Implements hook_theme().
 */
function pagerer_theme() {
  $theme = [
    'pagerer' => [
      'render element' => 'pager',
    ],
    'pagerer_base' => [
      'render element' => 'pager',
    ],
    'pagerer_mini' => [
      'variables' => [
        'id' => NULL,
        'title' => NULL,
        'value' => 0,
        'min' => 0,
        'max' => 0,
        'step' => 0,
        'button' => 'auto',
        'state' => [],
      ],
    ],
    'pagerer_slider' => [
      'variables' => [
        'id' => NULL,
        'title' => NULL,
        'state' => [],
      ],
    ],
    'pagerer_slider_icon' => [
      'variables' => [
        'icon' => NULL,
      ],
    ],
    'pagerer_scrollpane_button' => [
      'variables' => [
        'scope' => '',
        'text' => NULL,
        'title' => NULL,
      ],
    ],
  ];
  return $theme;
}

/**
 * Implements hook_element_info_alter().
 *
 * Pagerer replaces the 'pager' theme defined in the 'pager' element type
 * with the 'pagerer' theme and defined preset, if set so in configuration.
 * This configuration setting impacts the rendered pages in case of change,
 * so its cache tags are associated.
 */
function pagerer_element_info_alter(array &$types) {
  if (isset($types['pager'])) {

    // Add a pre render callback to associate config:pagerer.settings to
    // cache tags.
    $types['pager']['#pre_render'][] = '\\Drupal\\pagerer\\PagererCache::preRenderPager';

    // If configured to replace core pager, alter the pager #type theme.
    $override_preset_id = \Drupal::config('pagerer.settings')
      ->get('core_override_preset');
    if ($override_preset_id !== 'core') {
      $types['pager']['#theme'] = 'pagerer';
      $types['pager']['#config'] = [
        'preset' => $override_preset_id,
      ];
    }
  }
}

/**
 * Prepares variables for pagerer multi-pane pager templates.
 *
 * Default template: pagerer.html.twig.
 *
 * @param array $variables
 *   An associative array containing:
 *   - pager: A render element containing:
 *     - #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.
 */
function template_preprocess_pagerer(array &$variables) {

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

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

  // Preprocess through the multipane pager plugin.
  $pager
    ->setRouteName($variables['pager']['#route_name'])
    ->setRouteParameters(isset($variables['pager']['#route_parameters']) ? $variables['pager']['#route_parameters'] : []);
  \Drupal::service('pagerer.style.manager')
    ->createInstance('multipane', $variables['pager']['#config'])
    ->setPager($pager)
    ->preprocess($variables);
}

/**
 * Prepares variables for pagerer base templates.
 *
 * Default template: pagerer-base.html.twig.
 *
 * @param 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.
 */
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';
}

Functions

Namesort descending Description
pagerer_element_info_alter Implements hook_element_info_alter().
pagerer_help Implements hook_help().
pagerer_theme Implements hook_theme().
template_preprocess_pagerer Prepares variables for pagerer multi-pane pager templates.
template_preprocess_pagerer_base Prepares variables for pagerer base templates.