You are here

class CleanPagerSubscriber in Clean Pagination 8

Hierarchy

  • class \Drupal\cleanpager\EventSubscriber\CleanPagerSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of CleanPagerSubscriber

1 file declares its use of CleanPagerSubscriber
CleanPager.php in src/StackMiddleware/CleanPager.php
1 string reference to 'CleanPagerSubscriber'
cleanpager.services.yml in ./cleanpager.services.yml
cleanpager.services.yml
1 service uses CleanPagerSubscriber
cleanpager_subscriber in ./cleanpager.services.yml
Drupal\cleanpager\EventSubscriber\CleanPagerSubscriber

File

src/EventSubscriber/CleanPagerSubscriber.php, line 18
Fto redirects.

Namespace

Drupal\cleanpager\EventSubscriber
View source
class CleanPagerSubscriber implements EventSubscriberInterface {

  /**
   * {@inheritdoc}
   */
  static function getSubscribedEvents() {

    // Replace ?page=1 to /page/1.
    $events[KernelEvents::REQUEST][] = array(
      'checkForRedirection',
    );
    return $events;
  }
  public function checkForRedirection(GetResponseEvent $event) {
    global $_cleanpager_rewritten;
    $path = \Drupal::service('path.current')
      ->getPath();
    if ($path_length = strpos($path, '/page/')) {
      $path_test_part = substr($path, 0, $path_length);
    }
    else {
      $path_test_part = $path;
    }
    $pages = \Drupal::configFactory()
      ->get('cleanpager.settings')
      ->get('cleanpager_pages');
    if (\Drupal::service('path.matcher')
      ->matchPath($path_test_part, $pages)) {

      // Pass along additional query string values.
      $query_values = $_GET;
      if (isset($query_values['page']) && !empty($query_values['page']) && $_cleanpager_rewritten == FALSE) {
        $path .= '/page/' . $query_values['page'];
        if (\Drupal::configFactory()
          ->get('cleanpager.settings')
          ->get('cleanpager_add_trailing')) {
          $path .= '/';
        }
        unset($query_values['page']);
        if (isset($query_values['q'])) {
          unset($query_values['q']);
        }
        $options['query'] = $query_values;
        $path .= (strpos($path, '?') !== FALSE ? '&' : '?') . $this
          ->cleanPagerHttpBuildQuery($options['query']);
        unset($_GET['page']);
        header('Location: ' . $path, FALSE, 302);
        exit;
      }
    }
  }
  public function cleanPagerHttpBuildQuery(array $query, $parent = '') {
    $params = array();
    foreach ($query as $key => $value) {
      $key = $parent ? $parent . '[' . rawurlencode($key) . ']' : rawurlencode($key);

      // Recurse into children.
      if (is_array($value)) {
        $params[] = $this
          ->cleanPagerHttpBuildQuery($value, $key);
      }
      elseif (!isset($value)) {
        $params[] = $key;
      }
      else {

        // For better readability of paths in query strings, we decode slashes.
        $params[] = $key . '=' . str_replace('%2F', '/', rawurlencode($value));
      }
    }
    return implode('&', $params);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CleanPagerSubscriber::checkForRedirection public function
CleanPagerSubscriber::cleanPagerHttpBuildQuery public function
CleanPagerSubscriber::getSubscribedEvents static function Returns an array of event names this subscriber wants to listen to.