FormTestEventSubscriber.php

Same filename in other branches
  1. 9 core/modules/system/tests/modules/form_test/src/EventSubscriber/FormTestEventSubscriber.php
  2. 8.9.x core/modules/system/tests/modules/form_test/src/EventSubscriber/FormTestEventSubscriber.php
  3. 10 core/modules/system/tests/modules/form_test/src/EventSubscriber/FormTestEventSubscriber.php

Namespace

Drupal\form_test\EventSubscriber

File

core/modules/system/tests/modules/form_test/src/EventSubscriber/FormTestEventSubscriber.php

View source
<?php

declare (strict_types=1);
namespace Drupal\form_test\EventSubscriber;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;

/**
 * Test event subscriber to add new attributes to the request.
 */
class FormTestEventSubscriber implements EventSubscriberInterface {
    
    /**
     * Adds custom attributes to the request object.
     *
     * @param \Symfony\Component\HttpKernel\Event\RequestEvent $event
     *   The kernel request event.
     */
    public function onKernelRequest(RequestEvent $event) {
        $request = $event->getRequest();
        $request->attributes
            ->set('custom_attributes', 'custom_value');
        $request->attributes
            ->set('request_attribute', 'request_value');
    }
    
    /**
     * Adds custom headers to the response.
     *
     * @param \Symfony\Component\HttpKernel\Event\ResponseEvent $event
     *   The kernel response event.
     */
    public function onKernelResponse(ResponseEvent $event) {
        $response = $event->getResponse();
        $response->headers
            ->set('X-Form-Test-Response-Event', 'invoked');
    }
    
    /**
     * {@inheritdoc}
     */
    public static function getSubscribedEvents() : array {
        $events[KernelEvents::REQUEST][] = [
            'onKernelRequest',
        ];
        $events[KernelEvents::RESPONSE][] = [
            'onKernelResponse',
        ];
        return $events;
    }

}

Classes

Title Deprecated Summary
FormTestEventSubscriber Test event subscriber to add new attributes to the request.

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