class ClientErrorResponseSubscriber

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/EventSubscriber/ClientErrorResponseSubscriber.php \Drupal\Core\EventSubscriber\ClientErrorResponseSubscriber
  2. 8.9.x core/lib/Drupal/Core/EventSubscriber/ClientErrorResponseSubscriber.php \Drupal\Core\EventSubscriber\ClientErrorResponseSubscriber
  3. 10 core/lib/Drupal/Core/EventSubscriber/ClientErrorResponseSubscriber.php \Drupal\Core\EventSubscriber\ClientErrorResponseSubscriber

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

Hierarchy

Expanded class hierarchy of ClientErrorResponseSubscriber

File

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

Namespace

Drupal\Core\EventSubscriber
View source
class ClientErrorResponseSubscriber implements EventSubscriberInterface {
    
    /**
     * Sets the '4xx-response' cache tag on 4xx responses.
     *
     * @param \Symfony\Component\HttpKernel\Event\ResponseEvent $event
     *   The event to process.
     */
    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);
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public static function getSubscribedEvents() : array {
        // Priority 10, so that it runs before FinishResponseSubscriber, which will
        // expose the cacheability metadata in the form of headers.
        $events[KernelEvents::RESPONSE][] = [
            'onRespond',
            10,
        ];
        return $events;
    }

}

Members

Title Sort descending Modifiers Object type Summary
ClientErrorResponseSubscriber::getSubscribedEvents public static function
ClientErrorResponseSubscriber::onRespond public function Sets the '4xx-response' cache tag on 4xx responses.

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