RedirectEventSubscriber.php

Namespace

Drupal\rules\EventSubscriber

File

src/EventSubscriber/RedirectEventSubscriber.php

View source
<?php

namespace Drupal\rules\EventSubscriber;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;

/**
 * Changes the response to a redirect, if a redirect rules action was executed .
 */
class RedirectEventSubscriber implements EventSubscriberInterface {
    
    /**
     * Checks if a redirect rules action was executed.
     *
     * Redirects to the provided url if there is one.
     *
     * @param \Symfony\Component\HttpKernel\Event\ResponseEvent $event
     *   The response event.
     */
    public function checkRedirectIssued(ResponseEvent $event) {
        $request = $event->getRequest();
        $redirect_url = $request->attributes
            ->get('_rules_redirect_action_url');
        if (isset($redirect_url)) {
            $event->setResponse(new RedirectResponse($redirect_url));
        }
    }
    
    /**
     * Registers the methods in this class that should be listeners.
     *
     * @return array
     *   An array of event listener definitions.
     */
    public static function getSubscribedEvents() {
        $events[KernelEvents::RESPONSE][] = [
            'checkRedirectIssued',
        ];
        return $events;
    }

}

Classes

Title Deprecated Summary
RedirectEventSubscriber Changes the response to a redirect, if a redirect rules action was executed .