You are here

public function AlphaPagination::getCharacters in Views Alpha Pagination 7.2

Retrieves the characters used to populate the pagination item list.

Return value

\AlphaPaginationCharacter[] An associative array containing AlphaPaginationCharacter objects, keyed by its value.

3 calls to AlphaPagination::getCharacters()
AlphaPagination::getLabel in src/AlphaPagination.php
Retrieves the proper label for a character.
AlphaPagination::getUrl in src/AlphaPagination.php
Retrieves the URL for the current view.
AlphaPagination::getValue in src/AlphaPagination.php
Retrieves the proper value for a character.

File

src/AlphaPagination.php, line 237

Class

AlphaPagination
A base views handler for alpha pagination.

Code

public function getCharacters() {

  /** @var \AlphaPaginationCharacter[] $characters */
  static $characters;
  if (!isset($characters)) {
    $all = $this
      ->getOption('paginate_all_display') === '1' ? $this
      ->getOption('paginate_all_label', t('All')) : '';
    $all_value = $this
      ->getOption('paginate_all_value', 'all');
    $numeric_label = $this
      ->getOption('paginate_numeric_label');
    $numeric_type = $this
      ->getOption('paginate_view_numbers', '0');
    $numeric_value = $this
      ->getOption('paginate_numeric_value');
    $numeric_divider = $numeric_type !== '2' && $this
      ->getOption('paginate_numeric_divider') ? [
      '-' => '',
    ] : [];

    // Check to see if this query is cached. If it is, then just pull our
    // results set from it so that things can move quickly through here. We're
    // caching in the event the view has a very large result set.
    $cid = $this
      ->getCid();
    if (($cache = cache_get($cid)) && !empty($cache->data)) {
      $characters = $cache->data;
    }
    else {

      // Add alphabet characters.
      foreach ($this
        ->getAlphabet() as $value) {
        $characters[$value] = $value;
      }

      // Append or prepend numeric item(s).
      $numeric = [];
      if ($numeric_type !== '0') {

        // Determine type of numeric items.
        if ($numeric_type === '2') {
          $numeric[$numeric_value] = check_plain($numeric_label);
        }
        else {
          foreach ($this
            ->getNumbers() as $value) {
            $numeric[$value] = $value;
          }
        }

        // Merge in numeric items.
        if ($this
          ->getOption('paginate_numeric_position') === 'after') {
          $characters = array_merge($characters, $numeric_divider, $numeric);
        }
        else {
          $characters = array_merge($numeric, $numeric_divider, $characters);
        }
      }

      // Append or prepend the "all" item.
      if ($all) {
        if ($this
          ->getOption('paginate_all_position') === 'before') {
          $characters = [
            $all_value => $all,
          ] + $characters;
        }
        else {
          $characters[$all_value] = $all;
        }
      }

      // Convert characters to objects.
      foreach ($characters as $value => $label) {
        $characters[$value] = new AlphaPaginationCharacter($this, $label, $value);
      }

      // Determine enabled prefixes.
      $prefixes = $this
        ->getEntityPrefixes();
      foreach ($prefixes as $value) {

        // Ensure numeric prefixes use the numeric label, if necessary.
        if ($this
          ->isNumeric($value) && $numeric_type === '2') {
          $value = $numeric_value;
        }
        if (isset($characters[$value])) {
          $characters[$value]
            ->setEnabled(TRUE);
        }
      }

      // Remove all empty prefixes.
      if (!$this
        ->getOption('paginate_toggle_empty')) {

        // Ensure "all" and numeric divider objects aren't removed.
        if ($all) {
          $prefixes[] = $all_value;
        }
        if ($numeric_divider) {
          $prefixes[] = '-';
        }

        // Determine if numeric results are not empty.
        $characters = array_filter(array_intersect_key($characters, array_flip($prefixes)));
      }

      // Remove all numeric values if they're all empty.
      if ($this
        ->getOption('paginate_numeric_hide_empty')) {

        // Determine if numeric results are not empty.
        $numeric_results = array_filter(array_intersect_key($characters, array_flip($numeric)));
        if (!$numeric_results) {
          $characters = array_diff_key($characters, array_flip($numeric));
        }
      }

      // Cache the results.
      cache_set($cid, $characters, 'cache');
    }

    // Default current value to "all", if enabled, or the first character.
    $current = $all ? $all_value : '';

    // Attempt to determine if a valid argument was provided.
    $arg_count = count($this->handler->view->args);
    if ($arg_count) {
      $arg = $this->handler->view->args[$arg_count - 1];
      if ($arg && in_array($arg, array_keys($characters))) {
        $current = $arg;
      }
    }

    // Determine the first active character.
    if ($current) {
      foreach ($characters as $character) {
        if ($character
          ->isNumeric() && $numeric_type === '2' ? $numeric_value === $current : $character
          ->getValue() === $current) {
          $character
            ->setActive(TRUE);
          break;
        }
      }
    }
  }
  return $characters;
}