Score:1

Injecting service into another service does not work

jm flag

I have to override pagecache service in order to have some custom validation logic to be executed for anonymous users. The issue is that I have the domain module's service domain.negotiator to be injected into pagecache service, but when I call the domain.negotiator service it is always null no matter what I do. I think it has to do with something with priority. I can confirm the overridden service is being executed.

Code to override PageCache service

/**
   * {@inheritdoc}
   */
  public function alter(ContainerBuilder $container) {
    // Overrides language_manager class to test domain language negotiation.
    $definition = $container->getDefinition('http_middleware.page_cache');
    $definition->setClass('Drupal\hcl_login\StackMiddleware\StaticCache')->addArgument(
      new Reference('domain.negotiator'),
    );
  }

Overridden service

/**
   * {@inheritdoc}
   */
  public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE): Response {
    $domain = \Drupal::service('domain.negotiator');
    $x = $this->domainNegotiator->getActiveDomain();
    $currentDomain = \Drupal::service('entity_type.manager')->getStorage('domain')->createHostname();
    $response = parent::handle($request, $type, $catch);
    return $response;
  }

4uk4 avatar
cn flag
Even if it looks like a service, a middleware is not a Drupal service, it runs outside of the Drupal kernel.
Jaswin Singh avatar
jm flag
Thanks, @4k4 for replying, Is there any workaround to achieve it. Initially, I was using subscribing to event KERNAL:: REQUEST to add the custom validation logic that needs to run on every single page for anonymous users, but because of drupal caching, Pages were cached and it was not achieveable.
4uk4 avatar
cn flag
Workaround for what? The page cache is like a reverse proxy caching based on the URL (including domain name and query string by the way). If this is not what you want then uninstall this module and use the dynamic page cache.
leymannx avatar
ne flag
What about `$request->getHost()` or `$request->getSchemeAndHttpHost()`?
Jaswin Singh avatar
jm flag
@leymannx Still, I would need user service to check which is always showing anonymous users, I tried with event subscriber but when I set the priority to 210, I can't access the service.
Jaswin Singh avatar
jm flag
Another issue is that when using event subscribers, pages are being cached and custom validation is not being executed every time.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.