You are here

public function CleanPager::rewriteUrl in Clean Pagination 8

1 call to CleanPager::rewriteUrl()
CleanPager::handle in src/StackMiddleware/CleanPager.php
Handles a Request to convert it to a Response.

File

src/StackMiddleware/CleanPager.php, line 45

Class

CleanPager
Provides a HTTP middleware.

Namespace

Drupal\cleanpager\StackMiddleware

Code

public function rewriteUrl(Request $request, $q) {
  global $_cleanpager_rewritten;
  $q_array = explode('/', $q);
  if (\Drupal::configFactory()
    ->get('cleanpager.settings')
    ->get('cleanpager_add_trailing')) {
    array_pop($q_array);
  }
  if ($this
    ->cleanPagerIsPagerElement(end($q_array))) {
    $_cleanpager_rewritten = FALSE;
    $p = array_pop($q_array);
    if (end($q_array) == 'page') {
      array_pop($q_array);
      $_cleanpager_rewritten = TRUE;
      $q = implode('/', $q_array);
      $current_path = $request
        ->getPathInfo();
      $path_args = explode('/', $current_path);
      if ($path_args[1] == 'views' && $path_args[2] == 'ajax' && !empty($_POST['view_path'])) {
        $q = '/views/ajax';
      }
      else {
        $_REQUEST['q'] = $_GET['q'] = $q;
        $request->query
          ->set('q', $q);
      }
      $_REQUEST['page'] = $_GET['page'] = $p;
      $request->server
        ->set('REQUEST_URI', $q);
      $request->server
        ->set('REDIRECT_URL', $q);
      $query_string = $request->server
        ->get('QUERY_STRING', $q);
      $request->server
        ->set('QUERY_STRING', $query_string . '&page=' . $p);
      $request->query
        ->add([
        'page' => $p,
      ]);
      $request
        ->initialize($request->query
        ->all(), $request->request
        ->all(), $request->attributes
        ->all(), $request->cookies
        ->all(), $request->files
        ->all(), $request->server
        ->all(), $request
        ->getContent());
    }
  }
  return $request;
}