-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix for issue #7242 : broken translations for enums in forms #7243
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
FoxCie
wants to merge
1
commit into
EasyCorp:4.x
Choose a base branch
from
FoxCie:issue_7242_broken_enum_translations_in_forms
base: 4.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| <?php | ||
|
|
||
| namespace EasyCorp\Bundle\EasyAdminBundle\Tests\Controller; | ||
|
|
||
| use Doctrine\ORM\EntityRepository; | ||
| use EasyCorp\Bundle\EasyAdminBundle\Test\AbstractCrudTestCase; | ||
| use EasyCorp\Bundle\EasyAdminBundle\Tests\TestApplication\Controller\DashboardController; | ||
| use EasyCorp\Bundle\EasyAdminBundle\Tests\TestApplication\Controller\FormEnumTranslationController; | ||
| use EasyCorp\Bundle\EasyAdminBundle\Tests\TestApplication\Entity\BlogPost; | ||
| use EasyCorp\Bundle\EasyAdminBundle\Tests\TestApplication\Enum\BlogPostStateEnum; | ||
| use Symfony\Contracts\Translation\TranslatorInterface; | ||
|
|
||
| class FormEnumTranslationControllerTest extends AbstractCrudTestCase | ||
| { | ||
| protected EntityRepository $blogPosts; | ||
|
|
||
| protected function setUp(): void | ||
| { | ||
| parent::setUp(); | ||
| $this->client->followRedirects(); | ||
|
|
||
| $this->blogPosts = $this->entityManager->getRepository(BlogPost::class); | ||
| } | ||
|
|
||
| protected function getControllerFqcn(): string | ||
| { | ||
| return FormEnumTranslationController::class; | ||
| } | ||
|
|
||
| protected function getDashboardFqcn(): string | ||
| { | ||
| return DashboardController::class; | ||
| } | ||
|
|
||
| public function testFieldsFormatValue() | ||
| { | ||
| $translator = static::getContainer()->get(TranslatorInterface::class); | ||
| static::assertInstanceOf(TranslatorInterface::class, $translator); | ||
| $this->client->request('GET', $this->generateNewFormUrl()); | ||
|
|
||
| foreach (BlogPostStateEnum::cases() as $case) { | ||
| self::assertAnySelectorTextSame('option', $case->trans($translator, locale: 'en')); | ||
| } | ||
| } | ||
| } |
27 changes: 27 additions & 0 deletions
27
tests/TestApplication/src/Controller/FormEnumTranslationController.php
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| <?php | ||
|
|
||
| namespace EasyCorp\Bundle\EasyAdminBundle\Tests\TestApplication\Controller; | ||
|
|
||
| use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController; | ||
| use EasyCorp\Bundle\EasyAdminBundle\Field\ChoiceField; | ||
| use EasyCorp\Bundle\EasyAdminBundle\Tests\TestApplication\Entity\BlogPost; | ||
|
|
||
| /** | ||
| * @extends AbstractCrudController<BlogPost> | ||
| */ | ||
| class FormEnumTranslationController extends AbstractCrudController | ||
| { | ||
| public static function getEntityFqcn(): string | ||
| { | ||
| return BlogPost::class; | ||
| } | ||
|
|
||
| public function configureFields(string $pageName): iterable | ||
| { | ||
| // Test translating enums in forms. When enums implement TranslatableInterface, this should be done by Symfony | ||
| // automagically. | ||
| return [ | ||
| ChoiceField::new('state', 'State'), | ||
| ]; | ||
| } | ||
| } |
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| <?php | ||
|
|
||
| namespace EasyCorp\Bundle\EasyAdminBundle\Tests\TestApplication\Enum; | ||
|
|
||
| use Symfony\Contracts\Translation\TranslatableInterface; | ||
| use Symfony\Contracts\Translation\TranslatorInterface; | ||
|
|
||
| enum BlogPostStateEnum: string implements TranslatableInterface | ||
| { | ||
| case Draft = 'draft'; | ||
| case Published = 'published'; | ||
| case Deleted = 'deleted'; | ||
|
|
||
| public function trans(TranslatorInterface $translator, ?string $locale = null): string | ||
| { | ||
| return match ($this) { | ||
| self::Draft => $translator->trans('BlogPostStateEnum.draft', locale: $locale), | ||
| self::Published => $translator->trans('BlogPostStateEnum.published', locale: $locale), | ||
| self::Deleted => $translator->trans('BlogPostStateEnum.deleted', locale: $locale), | ||
| }; | ||
| } | ||
| } |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This sounds like a bug in Symfony?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They acknowledged that the previous behaviour was actually the bug. When dealing with choices, the keys of the array should be the labels. It is not explicit on EnumType, but on its parent ChoiceType, the doc states that clearly :

Source : https://symfony.com/doc/current/reference/forms/types/choice.html#choices .
The thing is that it was actually impossible to change an enum's labels with the choices array with the previous behaviour. Now it is possible, but the old behaviour is still present if the keys are integers (e.g. when giving the
::cases()as the choices).EDIT : the associated bug in Symfony is here : symfony/symfony#61927 .
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This still looks wrong to me. The
EnumTypeshould transform the data to make it compatible withChoiceTypeThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is what is done. It is just the label that can be changed by passing a string value as the key of the array. EnumType does what should be done so that the values are properly handled and converted. If you think this is not appropriate, you could open an issue on the Symfony side.