You are here

public function AlphaPagination::getUrl in Views Alpha Pagination 8.2

Retrieves the URL for the current view.

Note: this follows very similarly to \view::get_url to process arguments, however it is in fact severely modified to account for characters appended by this module.

Return value

string The URL for the view or current_path().

1 call to AlphaPagination::getUrl()
AlphaPagination::getTokens in src/AlphaPagination.php
Provides the token data that is passed when during replacement.

File

src/AlphaPagination.php, line 751

Class

AlphaPagination
A base views handler for alpha pagination.

Namespace

Drupal\alpha_pagination

Code

public function getUrl() {
  if (empty($this->url)) {
    if (!empty($this->handler->view->override_url)) {
      $this->url = $this->handler->view->override_url;
      return $this->url;
    }
    $path = $this->handler->view
      ->getPath();
    $args = $this->handler->view->args;

    // Exclude arguments that were computed, not passed on the URL.
    $position = 0;
    if (!empty($this->handler->view->argument)) {
      foreach ($this->handler->view->argument as $argument_id => $argument) {
        if (!empty($argument->options['default_argument_skip_url'])) {
          unset($args[$position]);
        }
        $position++;
      }
    }

    // Don't bother working if there's nothing to do:
    if (empty($path) || empty($args) && strpos($path, '%') === FALSE) {
      $path = \Drupal::service('path.current')
        ->getPath();
      $pieces = explode('/', $path);

      // If getPath returns heading slash, remove it as it will ba
      // added later.
      if ($pieces[0] == '') {
        array_shift($pieces);
      }
      if (array_key_exists(end($pieces), $this
        ->getCharacters())) {
        array_pop($pieces);
      }
      $this->url = implode('/', $pieces);
      return $this->url;
    }
    $pieces = [];
    $argument_keys = isset($this->handler->view->argument) ? array_keys($this->handler->view->argument) : [];
    $id = current($argument_keys);
    foreach (explode('/', $path) as $piece) {
      if ($piece != '%') {
        $pieces[] = $piece;
      }
      else {
        if (empty($args)) {

          // Try to never put % in a url; use the wildcard instead.
          if ($id && !empty($this->handler->view->argument[$id]->options['exception']['value'])) {
            $pieces[] = $this->handler->view->argument[$id]->options['exception']['value'];
          }
          else {

            // Gotta put something if there just isn't one.
            $pieces[] = '*';
          }
        }
        else {
          $pieces[] = array_shift($args);
        }
        if ($id) {
          $id = next($argument_keys);
        }
      }
    }

    // Just return the computed pieces, don't merge any extra remaining args.
    $this->url = implode('/', $pieces);
  }
  return $this->url;
}