diff --git a/src/Filter/Configurator/CommonConfigurator.php b/src/Filter/Configurator/CommonConfigurator.php index 1516d41487..1eaa4c0b82 100644 --- a/src/Filter/Configurator/CommonConfigurator.php +++ b/src/Filter/Configurator/CommonConfigurator.php @@ -8,7 +8,8 @@ use EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto; use EasyCorp\Bundle\EasyAdminBundle\Dto\FilterDto; use EasyCorp\Bundle\EasyAdminBundle\Generator\LabelGenerator; -use Symfony\Component\Translation\TranslatableMessage; +use Symfony\Contracts\Translation\TranslatableInterface; +use function Symfony\Component\Translation\t; /** * @author Javier Eguiluz @@ -24,10 +25,16 @@ public function configure(FilterDto $filterDto, ?FieldDto $fieldDto, EntityDto $ { if (null === $filterDto->getLabel()) { $fieldLabel = $fieldDto?->getLabel(); - if ($fieldLabel instanceof TranslatableMessage) { - $fieldLabel = $fieldLabel->getMessage(); + $translationDomain = $context->getI18n()->getTranslationDomain(); + + if ($fieldLabel instanceof TranslatableInterface) { + $label = $fieldLabel; + } elseif (null !== $fieldLabel) { + $label = t($fieldLabel, [], $translationDomain); + } else { + $label = t(LabelGenerator::humanize($filterDto->getProperty()), [], $translationDomain); } - $label = $fieldLabel ?? LabelGenerator::humanize($filterDto->getProperty()); + $filterDto->setLabel($label); } } diff --git a/tests/Unit/Filter/Configurator/CommonConfiguratorTest.php b/tests/Unit/Filter/Configurator/CommonConfiguratorTest.php new file mode 100644 index 0000000000..81c97dc1e7 --- /dev/null +++ b/tests/Unit/Filter/Configurator/CommonConfiguratorTest.php @@ -0,0 +1,125 @@ +setIdentifier(['id']); + $this->entityDto = new EntityDto('App\Entity\Product', $metadata); + $this->configurator = new CommonConfigurator(); + } + + public function testSupportsAllFilters(): void + { + $filter = TextFilter::new('name'); + $filterDto = $filter->getAsDto(); + $adminContext = $this->createAdminContext(); + + $this->assertTrue($this->configurator->supports($filterDto, null, $this->entityDto, $adminContext)); + } + + public function testConfigureUsesFieldLabelAndWrapsInTranslatable(): void + { + $filter = TextFilter::new('name'); + $filterDto = $filter->getAsDto(); + + $field = TextField::new('name', 'Product Name'); + $fieldDto = $field->getAsDto(); + + $adminContext = $this->createAdminContext(); + + $this->configurator->configure($filterDto, $fieldDto, $this->entityDto, $adminContext); + + $this->assertInstanceOf(TranslatableInterface::class, $filterDto->getLabel()); + } + + public function testConfigurePreservesTranslatableInterfaceFromField(): void + { + $filter = TextFilter::new('name'); + $filterDto = $filter->getAsDto(); + + $translatableLabel = new TranslatableMessage('product.name', [], 'messages'); + $field = TextField::new('name'); + $fieldDto = $field->getAsDto(); + $fieldDto->setLabel($translatableLabel); + + $adminContext = $this->createAdminContext(); + + $this->configurator->configure($filterDto, $fieldDto, $this->entityDto, $adminContext); + + $this->assertSame($translatableLabel, $filterDto->getLabel()); + } + + public function testConfigureHumanizesLabelAndWrapsInTranslatable(): void + { + $filter = TextFilter::new('productName'); + $filterDto = $filter->getAsDto(); + + $adminContext = $this->createAdminContext(); + + $this->configurator->configure($filterDto, null, $this->entityDto, $adminContext); + + $label = $filterDto->getLabel(); + $this->assertInstanceOf(TranslatableInterface::class, $label); + $this->assertSame('Product Name', $label->getMessage()); + } + + public function testConfigureUsesCorrectTranslationDomain(): void + { + $filter = TextFilter::new('name'); + $filterDto = $filter->getAsDto(); + + $field = TextField::new('name', 'custom.translation.key'); + $fieldDto = $field->getAsDto(); + + $adminContext = $this->createAdminContext('my_domain'); + + $this->configurator->configure($filterDto, $fieldDto, $this->entityDto, $adminContext); + + $label = $filterDto->getLabel(); + $this->assertInstanceOf(TranslatableMessage::class, $label); + $this->assertSame('custom.translation.key', $label->getMessage()); + $this->assertSame('my_domain', $label->getDomain()); + } + + public function testConfigureDoesNotOverrideExplicitFilterLabel(): void + { + $filter = TextFilter::new('name')->setLabel('Custom Label'); + $filterDto = $filter->getAsDto(); + + $adminContext = $this->createAdminContext(); + + $this->configurator->configure($filterDto, null, $this->entityDto, $adminContext); + + $this->assertSame('Custom Label', $filterDto->getLabel()); + } + + private function createAdminContext(string $translationDomain = 'messages'): AdminContext + { + return AdminContext::forTesting( + RequestContext::forTesting(new Request()), + null, + null, + I18nContext::forTesting('en', 'ltr', $translationDomain) + ); + } +}