You are here

public function ClientErrorResponseSubscriber::onRespond in Drupal 8

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/EventSubscriber/ClientErrorResponseSubscriber.php \Drupal\Core\EventSubscriber\ClientErrorResponseSubscriber::onRespond()
  2. 10 core/lib/Drupal/Core/EventSubscriber/ClientErrorResponseSubscriber.php \Drupal\Core\EventSubscriber\ClientErrorResponseSubscriber::onRespond()

Sets the '4xx-response' cache tag on 4xx responses.

Parameters

\Symfony\Component\HttpKernel\Event\FilterResponseEvent $event: The event to process.

File

core/lib/Drupal/Core/EventSubscriber/ClientErrorResponseSubscriber.php, line 22

Class

ClientErrorResponseSubscriber
Response subscriber to set the '4xx-response' cache tag on 4xx responses.

Namespace

Drupal\Core\EventSubscriber

Code

public function onRespond(FilterResponseEvent $event) {
  if (!$event
    ->isMasterRequest()) {
    return;
  }
  $response = $event
    ->getResponse();
  if (!$response instanceof CacheableResponseInterface) {
    return;
  }
  if ($response
    ->isClientError()) {
    $http_4xx_response_cacheability = new CacheableMetadata();
    $http_4xx_response_cacheability
      ->setCacheTags([
      '4xx-response',
    ]);
    $response
      ->addCacheableDependency($http_4xx_response_cacheability);
  }
}