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;
}