Open
Conversation
It is already possible to use PHP enums in Fluid templates: Both
ViewHelpers and components can define an enum as argument type by using
the full name of the enum. With the `<f:constant>` ViewHelper, it is
possible to access individual enum cases. And enum cases are just
special PHP objects, which is why their properties (such as "name" or
"value") can be accessed with Fluid's variable syntax.
This patch aims to make the usage of enums for ViewHelper and
components easier: If an argument type is a known enum, Fluid
automatically tries to convert the supplied value to the matching enum
case. This works both for basic and backed enums: For backed enums,
Fluid first tries to find the enum case by its backed value and falls
back to the enum name. For basic enums, only the name is considered.
If the supplied value cannot be converted, the value remains unchanged,
which leads to the normal validation exception for a non-matching
argument type.
Given the following enum and component:
```php
namespace Vendor\Package;
enum VariantEnum: int
{
case BASIC = 123456;
}
```
```xml
<f:argument name="variant" type="Vendor\Package\VariantEnum" />
Selected variant: {variant.value}
```
Before this change, the `<f:constant>` ViewHelper needed to be used
if the value isn't already an enum:
```xml
<my:exampleComponent
variant="{f:constant(name: 'Vendor\Package\VariantEnum::BASIC')}"
/>
```
With this change, the name of the enum case is sufficient:
```xml
<my:exampleComponent
variant="BASIC"
/>
```
And because the enum is int-backed, this also works:
```xml
<my:exampleComponent
variant="123456"
/>
```
Contributor
Author
|
Related: #1044 |
Kanti
reviewed
Mar 13, 2026
| 'expectedValidity' => false, | ||
| 'expectedProcessedValue' => IntBackedEnumExample::BAR, | ||
| 'expectedProcessedValidity' => true, | ||
| ]; |
Contributor
There was a problem hiding this comment.
We should also test for string with int value inside.
Suggested change
| ]; | |
| ]; | |
| yield [ | |
| 'type' => IntBackedEnumExample::class, | |
| 'value' => '123', | |
| 'expectedValidity' => false, | |
| 'expectedProcessedValue' => IntBackedEnumExample::BAR, | |
| 'expectedProcessedValidity' => true, | |
| ]; |
Contributor
|
IMO the following should also be possible: <my:exampleComponent
variant="Vendor\Package\VariantEnum::BASIC"
/>This would keep the usage of the enum visible in Fluid whereas just |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
It is already possible to use PHP enums in Fluid templates: Both
ViewHelpers and components can define an enum as argument type by using
the full name of the enum. With the
<f:constant>ViewHelper, it ispossible to access individual enum cases. And enum cases are just
special PHP objects, which is why their properties (such as "name" or
"value") can be accessed with Fluid's variable syntax.
This patch aims to make the usage of enums for ViewHelper and
components easier: If an argument type is a known enum, Fluid
automatically tries to convert the supplied value to the matching enum
case. This works both for basic and backed enums: For backed enums,
Fluid first tries to find the enum case by its backed value and falls
back to the enum name. For basic enums, only the name is considered.
If the supplied value cannot be converted, the value remains unchanged,
which leads to the normal validation exception for a non-matching
argument type.
Given the following enum and component:
Before this change, the
<f:constant>ViewHelper needed to be usedif the value isn't already an enum:
With this change, the name of the enum case is sufficient:
And because the enum is int-backed, this also works: