You are here

public function QueryPathEventSubscriber::checkRedirection in Opigno module 8

Same name and namespace in other branches
  1. 3.x src/EventSubscriber/QueryPathEventSubscriber.php \Drupal\opigno_module\EventSubscriber\QueryPathEventSubscriber::checkRedirection()

File

src/EventSubscriber/QueryPathEventSubscriber.php, line 14

Class

QueryPathEventSubscriber

Namespace

Drupal\opigno_module\EventSubscriber

Code

public function checkRedirection(FilterResponseEvent $event) {

  // Add new routes for enable logic
  $available_routes = [
    'entity.opigno_activity.collection',
    'entity.opigno_module.collection',
    'entity.group.collection',
  ];
  $route = \Drupal::routeMatch();
  $session = \Drupal::request()
    ->getSession();
  if (in_array($route
    ->getRouteName(), $available_routes)) {
    $param = \Drupal::request()->query
      ->all();

    // If we have an empty page (without new query params), we load previous one
    if (empty($param)) {
      $order_values = $session
        ->get($route
        ->getRouteName());
      if (!empty($order_values)) {
        $url = Url::fromRoute($route
          ->getRouteName());
        $url
          ->setOption('query', $order_values);
        $response = new RedirectResponse($url
          ->toString());
        $event
          ->setResponse($response);
      }
    }
    else {
      $session
        ->set($route
        ->getRouteName(), $param);
    }
  }
}