Thanks to visit codestin.com
Credit goes to github.com

Skip to content

sulu_snippet_load_by_area crashes when snippet is not translated in current locale #8474

@steeven-th

Description

@steeven-th
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

  1. Create a snippet and assign it to a snippet area (e.g., default_links)
  2. Publish the snippet only in one locale (e.g., FR)
  3. In a Twig template, call:
    {% set snippets = sulu_snippet_load_by_area('default_links', {'prop': 'prop'}) %}
  4. Navigate to a page in the untranslated locale (e.g., EN)
  5. 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.

Metadata

Metadata

Assignees

Labels

BugError or unexpected behavior of already existing functionality

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions