Closed zyberspace closed 1 week ago
@VincentLanglet could you please have a look at this PR?
Will rebase as soon as #8215 is merged. Should fix the checks.
Hi, it can be rebased
@VincentLanglet Done. And thanks for fixing the build!
Can you approve the workflow a second time? Thanks!
Thanks @zyberspace
Support Symfony's
TranslatableInterface
in enum fieldsFixes the translation of enums implementing Symfony's
TranslatableInterface
.Right now there is a discrepancy between how the Symfony
EnumType
form type translates enums and how Sonata Admin translates them. TheEnumType
uses the enum case's name by default for the choice labels. If you need anything else, your enum has to implement theTranslatableInterface
and atrans()
method like in the third example on the docs page: https://symfony.com/doc/current/reference/forms/types/enum.html#example-usageHowever the
FieldDescriptionInterface::TYPE_ENUM
field type completely ignores this and instead implements the optionsuse_value
andenum_translation_domain
, which severely limits how the enum can be translated. I for example often use the formatenum.[ENUM_SHORT_NAME].[ENUM_VALUE]
which is not possible with the current implementation.With this PR, the
FieldDescriptionInterface::TYPE_ENUM
field type will use the enum'strans()
method if available to translate the enum and therefore use the same logic, Symfony'sEnumType
uses.With this PR, using enums is as easy as:
I also added tests for the previous translation method, as there were none.
I am targeting the 4.x branch, because people using enums with the
TranslatableInterface
currently can not use the enum field type anyway, as it would use the wrong translation, so there should be no backwards compatible issues here.Changelog