I am trying to alter the existing 'Log In' URL so that it points to a certain location and also includes the REQUEST_URI as a query string that is not encoded. How do I do this?
Based on this article, Altering existing routes and adding new routes based on dynamic ones and this SE answer I can change the 'Log In' URL so it points to a different location (/login). What I can't seem to figure out is how to add a query string that isn't encoded.
This code:
$route->setPath('/login?destination=' . \Drupal::request()->server->get('REQUEST_URI'));
Gives me:
/login%3Fdestination%3Dpage2
What I want is:
/login?destination=page2
I have looked into RequestContext::setQueryString and Route::addOptions but can't seem to get anything to work.
Full code:
<?php
namespace Drupal\myModule\Routing;
use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\RouteCollection;
/**
* Listens to the dynamic route events.
*/
class RouteSubscriber extends RouteSubscriberBase {
/**
* {@inheritdoc}
*/
protected function alterRoutes(RouteCollection $collection) {
// Change path '/user/login' to '/login'.
if ($route = $collection->get('user.login')) {
//$route->setPath('/login');
$route->setPath('/login?destination=' . \Drupal::request()->server->get('REQUEST_URI'));
}
// Always deny access to '/user/logout'.
// Note that the second parameter of setRequirement() is a string.
if ($route = $collection->get('user.logout')) {
$route->setRequirement('_access', 'FALSE');
}
}
}
href
via JS