You are here

public function CleanPagerSubscriber::checkForRedirection in Clean Pagination 8

File

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

Class

CleanPagerSubscriber

Namespace

Drupal\cleanpager\EventSubscriber

Code

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;
    }
  }
}