From 4fc55a777693c435c7cd3941d958acba1801a53a Mon Sep 17 00:00:00 2001 From: Simon Praetorius Date: Tue, 16 Dec 2025 17:32:38 +0100 Subject: [PATCH] [TASK] Allow TemplateValidator to discover underscore variables Fluid 5 disallows variable names that start with an underscore. While this is already enforced in `StandardVariableProvider` for setting such variables, this change now also triggers an exception if a variable with an invalid name is accessed. This is probably a temporary change to be removed with Fluid 6. For now, it allows the `TemplateValidator`, which is used during template warmup, to discover this issue during parse time, and to let users know about affected templates. Resolves: #1263 --- src/Core/Parser/SyntaxTree/ObjectAccessorNode.php | 7 +++++++ .../Validation/AccessToInvalidVariableName.fluid.html | 1 + tests/Functional/Validation/TemplateValidatorTest.php | 1 + 3 files changed, 9 insertions(+) create mode 100644 tests/Functional/Fixtures/Validation/AccessToInvalidVariableName.fluid.html diff --git a/src/Core/Parser/SyntaxTree/ObjectAccessorNode.php b/src/Core/Parser/SyntaxTree/ObjectAccessorNode.php index 0818b692a..3c4420bc3 100644 --- a/src/Core/Parser/SyntaxTree/ObjectAccessorNode.php +++ b/src/Core/Parser/SyntaxTree/ObjectAccessorNode.php @@ -11,6 +11,7 @@ use TYPO3Fluid\Fluid\Core\Compiler\TemplateCompiler; use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface; +use TYPO3Fluid\Fluid\Core\Variables\InvalidVariableIdentifierException; /** * A node which handles object access. This means it handles structures like {object.accessor.bla} @@ -34,6 +35,12 @@ final class ObjectAccessorNode extends AbstractNode */ public function __construct(string $objectPath) { + // TODO consider removing this exception in later Fluid versions. For now it allows the + // TemplateValidator to find occurrances of variables starting with "_" in templates, + // which are no longer allowed since Fluid 5. + if (str_starts_with($objectPath, '_') && $objectPath !== '_all') { + throw new InvalidVariableIdentifierException('Variable identifiers cannot start with a "_": ' . $objectPath, 1765900762); + } $this->objectPath = $objectPath; } diff --git a/tests/Functional/Fixtures/Validation/AccessToInvalidVariableName.fluid.html b/tests/Functional/Fixtures/Validation/AccessToInvalidVariableName.fluid.html new file mode 100644 index 000000000..04b2022b7 --- /dev/null +++ b/tests/Functional/Fixtures/Validation/AccessToInvalidVariableName.fluid.html @@ -0,0 +1 @@ +{_something.sub} diff --git a/tests/Functional/Validation/TemplateValidatorTest.php b/tests/Functional/Validation/TemplateValidatorTest.php index 2fca36b6d..816bf6df1 100644 --- a/tests/Functional/Validation/TemplateValidatorTest.php +++ b/tests/Functional/Validation/TemplateValidatorTest.php @@ -26,6 +26,7 @@ public static function validatorFindsParseErrorsDataProvider(): array [$fixturePath . 'InvalidNamespace.fluid.html', 'Unknown Namespace: foo', 0], [$fixturePath . 'RequiredArgumentMissing.fluid.html', 'Required argument "each" was not supplied.', 1237823699], [$fixturePath . 'RedefinedComponentArgument.fluid.html', 'Template argument "foo" has been defined multiple times.', 1744908509], + [$fixturePath . 'AccessToInvalidVariableName.fluid.html', 'Variable identifiers cannot start with a "_": _something.sub', 1765900762], ]; }