Alter existing ‘Log In’ route so URL includes request_uri as query string

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:


What I want is:


I have looked into RequestContext::setQueryString and Route::addOptions but can’t seem to get anything to work.

Full code:

namespace DrupalmyModuleRouting;

use DrupalCoreRoutingRouteSubscriberBase;
use SymfonyComponentRoutingRouteCollection;

* 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?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');