EnforcedFormResponseSubscriber.php

Same filename in other branches
  1. 8.9.x core/lib/Drupal/Core/EventSubscriber/EnforcedFormResponseSubscriber.php
  2. 10 core/lib/Drupal/Core/EventSubscriber/EnforcedFormResponseSubscriber.php
  3. 11.x core/lib/Drupal/Core/EventSubscriber/EnforcedFormResponseSubscriber.php

Namespace

Drupal\Core\EventSubscriber

File

core/lib/Drupal/Core/EventSubscriber/EnforcedFormResponseSubscriber.php

View source
<?php

namespace Drupal\Core\EventSubscriber;

use Drupal\Core\Form\EnforcedResponse;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\KernelEvents;

/**
 * Handle the EnforcedResponseException and deliver an EnforcedResponse.
 */
class EnforcedFormResponseSubscriber implements EventSubscriberInterface {
    
    /**
     * Replaces the response in case an EnforcedResponseException was thrown.
     */
    public function onKernelException(ExceptionEvent $event) {
        if ($response = EnforcedResponse::createFromException($event->getThrowable())) {
            // Setting the response stops the event propagation.
            $event->setResponse($response);
        }
    }
    
    /**
     * Unwraps an enforced response.
     */
    public function onKernelResponse(ResponseEvent $event) {
        $response = $event->getResponse();
        if ($response instanceof EnforcedResponse && $event->isMainRequest()) {
            $event->setResponse($response->getResponse());
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public static function getSubscribedEvents() {
        $events[KernelEvents::EXCEPTION] = [
            'onKernelException',
            128,
        ];
        $events[KernelEvents::RESPONSE] = [
            'onKernelResponse',
            128,
        ];
        return $events;
    }

}

Classes

Title Deprecated Summary
EnforcedFormResponseSubscriber Handle the EnforcedResponseException and deliver an EnforcedResponse.

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