You are here

public function ClientErrorResponseSubscriber::onRespond in Drupal 9

Same name and namespace in other branches
  1. 8 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\ResponseEvent $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(ResponseEvent $event) {
  if (!$event
    ->isMainRequest()) {
    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);
  }
}