You are here

class AjaxResponseSubscriber 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

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

Hierarchy

  • class \Drupal\views_ajax_get\EventSubscriber\AjaxResponseSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of AjaxResponseSubscriber

1 string reference to 'AjaxResponseSubscriber'
views_ajax_get.services.yml in ./views_ajax_get.services.yml
views_ajax_get.services.yml
1 service uses AjaxResponseSubscriber
views_ajax_get.ajax_response in ./views_ajax_get.services.yml
\Drupal\views_ajax_get\EventSubscriber\AjaxResponseSubscriber

File

src/EventSubscriber/AjaxResponseSubscriber.php, line 14

Namespace

Drupal\views_ajax_get\EventSubscriber
View source
class AjaxResponseSubscriber implements EventSubscriberInterface {
  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'));
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[KernelEvents::RESPONSE][] = [
      'onResponse',
      0,
    ];
    return $events;
  }

}

Members

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