You are here

protected function PagererStyleBase::prepareJsState in Pagerer 8

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

Prepares input parameters for a JS enabled pager widget.

This method stores the input state data for a Pagerer widget in the drupalSettings.

Parameters

array $state_settings: The array of state settings to be enriched and stored.

Return value

string the pagerer widget id to refer to in the drupalSettings.pagerer.state entry.

3 calls to PagererStyleBase::prepareJsState()
Mini::buildPagerItems in src/Plugin/pagerer/Mini.php
Return the pager render array.
Scrollpane::buildPagerItems in src/Plugin/pagerer/Scrollpane.php
Return the pager render array.
Slider::buildPagerItems in src/Plugin/pagerer/Slider.php
Return the pager render array.

File

src/Plugin/pagerer/PagererStyleBase.php, line 678

Class

PagererStyleBase
Base plugin for Pagerer.

Namespace

Drupal\pagerer\Plugin\pagerer

Code

protected function prepareJsState(array &$state_settings) {

  // Prepare query parameters.
  // In the 'page' querystring fragment, the current page is overridden
  // with a text that the js widget will then replace with the content of HTML
  // 'value' attribute.
  $query = $this->pager
    ->getQueryParameters($this->parameters, 'pagererpage');

  // Prepare the query string.
  $query_string = UrlHelper::buildQuery($query);

  // Are we displaying pages or items; 'value' HTML attribute will bear
  // the current $current value.
  if ($this
    ->getOption('display') == 'pages') {
    $current = $this->pager
      ->getCurrentPage() + 1;
    $interval = 1;
  }
  else {
    $current = $this->pager
      ->getLimit() * $this->pager
      ->getCurrentPage() + 1;
    $interval = $this->pager
      ->getLimit();
  }

  // Prepare js widget state.
  $default_settings = [
    'url' => $this->pager
      ->getHref([], NULL, NULL, FALSE)
      ->toString(),
    'queryString' => $query_string,
    'element' => $this->pager
      ->getElement(),
    'total' => $this->pager
      ->getTotalPages(),
    'totalItems' => $this->pager
      ->getTotalItems(),
    'current' => $this->pager
      ->getCurrentPage(),
    'interval' => $interval,
    'display' => $this
      ->getOption('display'),
    'value' => $current,
    'pageSeparator' => $this
      ->getOption('separator_display') ? $this
      ->getTag('page_separator') : 'none',
    'pageTag' => [
      'page_current' => $this
        ->getTag($this
        ->getOption('display') . '.page_current'),
      'page_previous' => $this
        ->getTag($this
        ->getOption('display') . '.page_previous'),
      'page_next' => $this
        ->getTag($this
        ->getOption('display') . '.page_next'),
    ],
  ];
  $state_settings = NestedArray::mergeDeep($default_settings, $state_settings);
  $random_generator = new Random();
  $pagerer_widget_id = 'pagerer-widget-' . $random_generator
    ->name(8, TRUE);
  return $pagerer_widget_id;
}