Problem
When a domain entity is loaded via $domain_storage->load() in a controller or service, it may return an entity from Drupal's static entity cache that was populated during early request negotiation — before alias resolution ran. The result: $domain->getHostname() returns the canonical (production) hostname instead of the aliased (local/staging) hostname.
This happens because hook_domain_load (implemented in DomainAliasHooks::domainLoad()) only fires when entities are loaded fresh from storage, not when they come from the static entity cache. During the request lifecycle:
- Domain negotiation loads all domains — they enter the static cache
- At this point,
hook_domain_loadfires but returns early because the active domain's alias property is not yet set - The alias negotiation event subscriber runs and sets the alias on the active domain
- Later code calls
$domain_storage->load('some_id')— the entity is returned from static cache, the hook does not fire, aliases are never applied
The existing test DomainAliasPrefixTest::testEnvironmentRewritePrefixedDomains() documents this issue with an explicit resetCache() call before reloading. But requiring every consumer to remember resetCache() is fragile and error-prone.
Additionally, path-prefixed domains that share a canonical hostname with another domain but have no alias of their own are never resolved when loaded from a different hostname context. The hook_domain_load tries loadByEnvironmentMatch() on the prefixed domain and finds no aliases.
Proposed fix
Replace hook_domain_load with alias resolution built into the storage layer:
- The domain_alias module swaps the domain entity storage via
hook_entity_type_alter(), replacingDomainStoragewithDomainAliasAwareDomainStorage - The new storage overrides
loadMultiple()to apply alias resolution afterparent::loadMultiple()— for ALL returned entities (cached or fresh) - Resolved entities are written back to the static cache so the resolution only happens once per entity per request
- A sibling fallback handles path-prefixed domains: when a domain has no aliases, aliases from domains sharing the same canonical hostname are checked
hook_domain_loadis removed fromDomainAliasHooks
Issue fork domain-3583190
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #3
mably commentedComment #7
mably commentedComment #16
mably commented