FormTestEventSubscriber.php in Drupal 8
File
core/modules/system/tests/modules/form_test/src/EventSubscriber/FormTestEventSubscriber.php
View source
<?php
namespace Drupal\form_test\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class FormTestEventSubscriber implements EventSubscriberInterface {
public function onKernelRequest(GetResponseEvent $event) {
$request = $event
->getRequest();
$request->attributes
->set('custom_attributes', 'custom_value');
$request->attributes
->set('request_attribute', 'request_value');
}
public function onKernelResponse(FilterResponseEvent $event) {
$response = $event
->getResponse();
$response->headers
->set('X-Form-Test-Response-Event', 'invoked');
}
public static function getSubscribedEvents() {
$events[KernelEvents::REQUEST][] = [
'onKernelRequest',
];
$events[KernelEvents::RESPONSE][] = [
'onKernelResponse',
];
return $events;
}
}