You are here

pagerer.module in Pagerer 8

Same filename and directory in other branches
  1. 8.2 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>';
  }
}

/**
 * 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']);

  // Preprocess through the multipane pager plugin.
  $pager = \Drupal::service('pagerer.factory')
    ->get($variables['pager']['#element'])
    ->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']);

  // 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 = \Drupal::service('pagerer.factory')
    ->get($variables['pager']['#element'])
    ->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.