Skip to content

Conversation

@s2b
Copy link
Contributor

@s2b s2b commented Dec 7, 2025

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:

namespace Vendor\Package;

enum VariantEnum: int
{
    case BASIC = 123456;
}
<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:

<my:exampleComponent
    variant="{f:constant(name: 'Vendor\Package\VariantEnum::BASIC')}"
/>

With this change, the name of the enum case is sufficient:

<my:exampleComponent
    variant="BASIC"
/>

And because the enum is int-backed, this also works:

<my:exampleComponent
    variant="123456"
/>

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"
/>
```
@s2b
Copy link
Contributor Author

s2b commented Dec 7, 2025

Related: #1044

@s2b s2b added the backport label Dec 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant