Support Symfony's TranslatableInterface
in enum fields
#8212
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.
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