You are here

public function Multipane::preprocess in Pagerer 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/pagerer/Multipane.php \Drupal\pagerer\Plugin\pagerer\Multipane::preprocess()

Prepares to render the pager.

Parameters

array $variables: An associative array containing:

  • style: The PagererStyle plugin id to be used to render the pager. Only for base style plugins.
  • element: An optional integer to distinguish between multiple pagers on one page.
  • parameters: An associative array of query string parameters to append to the pager links.
  • config: An associative array of configuration elements for the pager style.

Overrides PagererStyleInterface::preprocess

File

src/Plugin/pagerer/Multipane.php, line 43

Class

Multipane
A multi-pane (left, center, and right) pager style.

Namespace

Drupal\pagerer\Plugin\pagerer

Code

public function preprocess(array &$variables) {

  // Load preset if specified.
  if (!empty($this->configuration['preset'])) {
    $preset = PagererPreset::load($this->configuration['preset']);
  }

  // Fully qualify all panes.
  if (isset($preset)) {
    $cacheable = TRUE;
    foreach ([
      'left',
      'center',
      'right',
    ] as $pane) {

      // Determine pane's style.
      if ($preset_style = $preset
        ->getPaneData($pane, 'style')) {
        $this->configuration['panes'][$pane]['style'] = $preset_style;
      }

      // If we are overriding preset's configuration via passed in pane
      // variables, we can't cache on its config entity.
      if (!empty($this->configuration['panes'][$pane]['config'])) {
        $cacheable = FALSE;
      }

      // Determine pane's config.
      if ($preset_config = $preset
        ->getPaneData($pane, 'config')) {
        $this->configuration['panes'][$pane]['config'] = NestedArray::mergeDeep($preset_config, $this->configuration['panes'][$pane]['config']);
      }
    }
  }

  // Check if pager is needed; if not, return immediately.
  // It is the lowest required number of pages in any of the panes.
  $page_restriction = min(isset($this->configuration['panes']['left']['config']['display_restriction']) ? $this->configuration['panes']['left']['config']['display_restriction'] : 2, isset($this->configuration['panes']['center']['config']['display_restriction']) ? $this->configuration['panes']['center']['config']['display_restriction'] : 2, isset($this->configuration['panes']['right']['config']['display_restriction']) ? $this->configuration['panes']['right']['config']['display_restriction'] : 2);
  if ($this->pager
    ->getTotalPages() < $page_restriction) {
    return;
  }

  // Build render array.
  foreach ([
    'left',
    'center',
    'right',
  ] as $pane) {
    if ($this->configuration['panes'][$pane]['style'] != 'none') {
      $variables['pagerer'][$pane . '_pane'] = [
        '#type' => 'pager',
        '#theme' => 'pagerer_base',
        '#style' => $this->configuration['panes'][$pane]['style'],
        '#route_name' => $variables['pager']['#route_name'],
        '#route_parameters' => isset($variables['pager']['#route_parameters']) ? $variables['pager']['#route_parameters'] : [],
        '#element' => $variables['pager']['#element'],
        '#parameters' => $variables['pager']['#parameters'],
        '#config' => $this->configuration['panes'][$pane]['config'],
      ];
    }
    else {
      $variables['pagerer'][$pane . '_pane'] = [];
    }
  }

  // Add the preset entity cache metadata, if possible.
  if (isset($preset) && $cacheable) {
    CacheableMetadata::createFromRenderArray($variables)
      ->merge(CacheableMetadata::createFromObject($preset))
      ->applyTo($variables);
  }
}