-
Notifications
You must be signed in to change notification settings - Fork 353
Description
| Q | A |
|---|---|
| Sulu Version | 3.0.0 |
| PHP Version | 8.2 |
| DB Version | MySQL 8.0 |
| Browser Version | N/A (server-side error) |
Actual Behavior
When calling sulu_snippet_load_by_area() in a Twig template for a locale where the snippet content does not exist (e.g., snippet exists in FR but not in EN), an uncaught exception is thrown:
Locale must be a string
The error originates from ContentResolver::resolve() at line 46:
$locale = $dimensionContent->getLocale();
Assert::string($locale, 'Locale must be a string');Expected Behavior
The function should return null gracefully when the snippet content does not exist in the requested locale, similar to how it handles other missing cases (e.g., missing webspace, missing snippet area).
Steps to Reproduce
- Create a snippet and assign it to a snippet area (e.g.,
default_links) - Publish the snippet only in one locale (e.g., FR)
- In a Twig template, call:
{% set snippets = sulu_snippet_load_by_area('default_links', {'prop': 'prop'}) %} - Navigate to a page in the untranslated locale (e.g., EN)
- The page crashes with "Locale must be a string" error
Possible Solutions
Add a null check for the locale after ContentAggregator::aggregate() in SnippetAreaTwigExtension::loadSnippetByArea():
File: packages/snippet/src/Infrastructure/Symfony/Twig/SnippetAreaTwigExtension.php
// Line 84-91
$dimensionContent = $this->contentAggregator->aggregate(
$snippet,
[
'locale' => $locale,
'stage' => DimensionContentInterface::STAGE_LIVE,
'version' => DimensionContentInterface::CURRENT_VERSION,
]
);
// ADD THIS CHECK (between line 91 and 93)
if (null === $dimensionContent->getLocale()) {
return null;
}
// Line 93
$resolvedContent = $this->contentResolver->resolve($dimensionContent, $properties);This would be consistent with the existing null checks for $webspace, $localization, and $snippetArea earlier in the function.