3 namespace Drupal\Core\EventSubscriber;
5 use Drupal\Core\Cache\CacheableMetadata;
6 use Drupal\Core\Cache\CacheableResponseInterface;
7 use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
8 use Symfony\Component\HttpKernel\KernelEvents;
9 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
12 * Response subscriber to set the '4xx-response' cache tag on 4xx responses.
14 class ClientErrorResponseSubscriber implements EventSubscriberInterface {
17 * Sets the '4xx-response' cache tag on 4xx responses.
19 * @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event
20 * The event to process.
22 public function onRespond(FilterResponseEvent $event) {
23 if (!$event->isMasterRequest()) {
27 $response = $event->getResponse();
28 if (!$response instanceof CacheableResponseInterface) {
32 if ($response->isClientError()) {
33 $http_4xx_response_cacheability = new CacheableMetadata();
34 $http_4xx_response_cacheability->setCacheTags(['4xx-response']);
35 $response->addCacheableDependency($http_4xx_response_cacheability);
42 public static function getSubscribedEvents() {
43 // Priority 10, so that it runs before FinishResponseSubscriber, which will
44 // expose the cacheability metadata in the form of headers.
45 $events[KernelEvents::RESPONSE][] = ['onRespond', 10];