Skip to content

Instantly share code, notes, and snippets.

@nikathone
Created February 14, 2017 20:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nikathone/91420160b70073568f4083db3b029bee to your computer and use it in GitHub Desktop.
Save nikathone/91420160b70073568f4083db3b029bee to your computer and use it in GitHub Desktop.
User reset login subscriber and redirect alter example.
<?php
namespace Drupal\wh_signup\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
/**
* For more info check:
* - https://www.drupal.org/node/2385923
* - https://www.drupal.org/node/2023537
*/
class UserResetLoginSubscriber implements EventSubscriberInterface {
public function checkRedirection(FilterResponseEvent $event) {
$response = $event->getResponse();
$match = $response instanceof RedirectResponse ? self::getRouteMatchFromUri($response->getTargetUrl()) : [];
if ($macth && $match['_route'] == 'entity.user.edit_form') {
// Do your stuuf here like redirecting anywhere else
}
}
public static function getRouteMatchFromUri($url = '') {
$match = [];
if ($url) {
$request = Request::create($url);
$router = \Drupal::service('router.no_access_checks');
try {
$match = $router->matchRequest($request);
}
catch (\Exception $e) {
// No route found...
$match = [];
}
}
return $match;
}
static function getSubscribedEvents() {
$events[KernelEvents::RESPONSE][] = ['checkRedirection'];
return $events;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment