You are here

public function AjaxResponseSubscriber::onResponse in Views Ajax Get 8

Same name and namespace in other branches
  1. 2.0.x src/EventSubscriber/AjaxResponseSubscriber.php \Drupal\views_ajax_get\EventSubscriber\AjaxResponseSubscriber::onResponse()

File

src/EventSubscriber/AjaxResponseSubscriber.php, line 16

Class

AjaxResponseSubscriber
Adding the headers for cache control so that these requests can be cached.

Namespace

Drupal\views_ajax_get\EventSubscriber

Code

public function onResponse(FilterResponseEvent $event) {
  $response = $event
    ->getResponse();
  if (!$response instanceof ViewAjaxResponse) {
    return;
  }
  $request = $event
    ->getRequest();
  if (RouteMatch::createFromRequest($request)
    ->getRouteName() === 'views.ajax') {

    /** @var \Drupal\views\ViewExecutable $view */
    $view = $response
      ->getView();

    // If view is excluded from conversion, don't add cache headers.
    if (_views_ajax_get_is_ajax_get_view($view)) {
      $view
        ->getRequest()->headers
        ->set('Cache-Control', 'public, max-age=' . \Drupal::config('system_performance_settings')
        ->get('cache.page.max_age'));
    }
  }
}