I am trying to add the query string with the each URL access through the browser. To accomplish it, trying to implement that through the service. But getting no solution in Drupal 9.
mymodule.services.yml:
services:
weplant.userlibraryurl:
class: Drupal\weplant\EventSubscriber\UserLibrarySubscriber
arguments: ['@url_helper', '@request_stack']
tags:
- { name: event_subscriber }
UserLibrarySubscriber.php
<?php
namespace Drupal\weplant\EventSubscriber;
use Drupal\Core\Url;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Routing\UrlGeneratorInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class UserLibrarySubscriber implements EventSubscriberInterface {
/**
* The URL Helper service.
*
* @var \Drupal\Core\UrlHelper
*/
protected $urlHelper;
/**
* The request stack.
*
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
protected $requestStack;
/**
* Constructs a new instance.
*
* @param \Drupal\Core\UrlHelper $url_helper
* The URL helper service.
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
* The request stack.
*/
public function __construct(UrlHelper $url_helper, RequestStack $request_stack) {
$this->urlHelper = $url_helper;
$this->requestStack = $request_stack;
}
/**
* Modifies the URLs before they are processed.
*/
public function modifyUrls(GetResponseEvent $event) {
$request = $event->getRequest();
$current_url = Url::fromUri($request->getUri());
$query_params = $current_url->getOption('query') ?? [];
$query_params['my_param'] = 'my_value';
$current_url->setOption('query', $query_params);
$modified_url = $this->urlHelper->toString($current_url);
$request->server->set('REQUEST_URI', $modified_url);
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events[KernelEvents::REQUEST][] = ['modifyUrls'];
return $events;
}
}
?>
Error:
The website encountered an unexpected error. Please try again later.
Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException: The service "weplant.userlibraryurl" has a dependency on a non-existent service "url_helper". in Symfony\Component\DependencyInjection\Compiler\CheckExceptionOnInvalidReferenceBehaviorPass->processValue() (line 86 of /Users/test/Sites/auth8/vendor/symfony/dependency-injection/Compiler/CheckExceptionOnInvalidReferenceBehaviorPass.php).
Symfony\Component\DependencyInjection\Compiler\CheckExceptionOnInvalidReferenceBehaviorPass->processValue(Object, ) (Line: 83)
Symfony\Component\DependencyInjection\Compiler\AbstractRecursivePass->processValue(Array, ) (Line: 49)
Symfony\Component\DependencyInjection\Compiler\CheckExceptionOnInvalidReferenceBehaviorPass->processValue(Array) (Line: 92)
Symfony\Component\DependencyInjection\Compiler\AbstractRecursivePass->processValue(Object, 1) (Line: 49)
Symfony\Component\DependencyInjection\Compiler\CheckExceptionOnInvalidReferenceBehaviorPass->processValue(Object, 1) (Line: 83)
Symfony\Component\DependencyInjection\Compiler\AbstractRecursivePass->processValue(Array, 1) (Line: 49)
Symfony\Component\DependencyInjection\Compiler\CheckExceptionOnInvalidReferenceBehaviorPass->processValue(Array, 1) (Line: 47)
Symfony\Component\DependencyInjection\Compiler\AbstractRecursivePass->process(Object) (Line: 40)
Symfony\Component\DependencyInjection\Compiler\CheckExceptionOnInvalidReferenceBehaviorPass->process(Object) (Line: 94)
Symfony\Component\DependencyInjection\Compiler\Compiler->compile(Object) (Line: 762)
Symfony\Component\DependencyInjection\ContainerBuilder->compile() (Line: 1339)
Drupal\Core\DrupalKernel->compileContainer() (Line: 943)
Drupal\Core\DrupalKernel->initializeContainer() (Line: 482)
Drupal\Core\DrupalKernel->boot() (Line: 711)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)