function ClientErrorResponseSubscriber::onRespond

Same name in other branches
  1. 9 core/lib/Drupal/Core/EventSubscriber/ClientErrorResponseSubscriber.php \Drupal\Core\EventSubscriber\ClientErrorResponseSubscriber::onRespond()
  2. 8.9.x core/lib/Drupal/Core/EventSubscriber/ClientErrorResponseSubscriber.php \Drupal\Core\EventSubscriber\ClientErrorResponseSubscriber::onRespond()
  3. 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\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);
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.