Created
February 14, 2017 20:32
-
-
Save nikathone/91420160b70073568f4083db3b029bee to your computer and use it in GitHub Desktop.
User reset login subscriber and redirect alter example.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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