Unexpected locale fallback to first-defined portal localization in Sulu webspace resolution
0 reputation · 02 Sept 2021, 16:56 UTC
0 reputation · 02 Sept 2021, 16:56 UTC
Sulu resolves content through a webspace → portal → localization hierarchy. When a request locale does not match any portal localization, the system silently falls back to the first localization defined in that portal’s localizations array.
The admin UI “Default” checkbox on a localization affects only content-editing preselection, not frontend resolution. Editors expect the flagged localization to serve as the fallback, but the hard-coded strategy uses array order instead. This behavior is documented but non-obvious, and no configuration option or event hook exists to change the fallback strategy (e.g., to prefer a specific locale, log the fallback, or return 404) without overriding core controller services.
35275 reputation · 03 Sept 2021, 00:05 UTC
Sulu does not expose an event subscriber that fires before the first-defined localization fallback is applied. The supported extension point is a service decorator for Sulu\Bundle\WebspaceBundle\Resolver\WebspaceResolver. Portal localization order is statically defined in webspace XML and cannot be reordered at runtime through a custom portal provider without modifying the XML or overriding the resolver. If a future Sulu release adds a configurable fallback strategy, existing webspace definitions are expected to preserve their current array-order behavior unless the XML is explicitly updated.
Confirmed: The WebspaceResolver iterates the portal's localizations array and returns the first entry when no requested locale matches. The admin UI "Default" flag affects only content-editing preselection, not frontend resolution.
Likely explanation: Editors expect the "Default" checkbox to control frontend fallback because it mirrors similar concepts in other platforms. In Sulu, the array order in the webspace XML is the sole determinant of fallback precedence.
Because no pre-fallback event exists, decorate the resolver service. This preserves the original contract while letting you intercept, log, or alter the outcome.
WebspaceResolverInterface.resolve() to inspect the requested locale before delegating to the inner resolver.bin/console cache:clear.Within the decorator you can log the fallback, redirect to a preferred locale, or throw a NotFoundHttpException. If you choose a 404, update your sitemap and inform search engines, because previously indexed fallback URLs will change behavior.
There is no supported custom portal provider that dynamically reorders localizations at runtime. The webspace XML is the source of truth. Changing the array order requires editing the XML and clearing the cache; it is not a runtime operation.
If Sulu later introduces a configurable fallback strategy, backward compatibility is expected to preserve array-order behavior for existing webspace definitions. Adoption would require adding new XML attributes or configuration keys to opt into the new strategy. No automatic migration of fallback semantics is anticipated.
Before deploying, verify the exact service ID and decorator registration syntax against your Sulu version, as service names have evolved between 2.x and 3.x. One diagnostic detail changes the recommendation: do you need the decorator to return a 404 for unmatched locales, or only to log the fallback? A 404 requires SEO coordination and sitemap updates; logging alone is lower risk.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.