From 76b9d533fea7ff761e08edb46b7e3a429dcd0ab9 Mon Sep 17 00:00:00 2001 From: Alexandar Bozhinov Date: Fri, 17 Feb 2023 21:48:41 +0200 Subject: [PATCH 1/3] Update ChoiceConfigurator.php Update FieldFactory.php Update ChoiceConfigurator.php Update ChoiceConfigurator.php Update ChoiceConfigurator.php Update ChoiceConfigurator.php Update ChoiceConfigurator.php Update Actions.php Update Actions.php Update ChoiceConfigurator.php --- src/Factory/FieldFactory.php | 6 ++++++ src/Field/Configurator/ChoiceConfigurator.php | 19 +++++++++++++------ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/Factory/FieldFactory.php b/src/Factory/FieldFactory.php index 70d6f95b4f..588b3e9fed 100644 --- a/src/Factory/FieldFactory.php +++ b/src/Factory/FieldFactory.php @@ -107,6 +107,12 @@ public function processFields(EntityDto $entityDto, FieldCollection $fields): vo $configurator->configure($fieldDto, $entityDto, $context); } + + if (null !== $currentPage && false === $fieldDto->isDisplayedOn($currentPage)) { + $fields->unset($fieldDto); + + continue; + } foreach ($fieldDto->getFormThemes() as $formThemePath) { $context?->getCrud()?->addFormTheme($formThemePath); diff --git a/src/Field/Configurator/ChoiceConfigurator.php b/src/Field/Configurator/ChoiceConfigurator.php index 104b5a4ace..d42040780c 100644 --- a/src/Field/Configurator/ChoiceConfigurator.php +++ b/src/Field/Configurator/ChoiceConfigurator.php @@ -10,6 +10,7 @@ use EasyCorp\Bundle\EasyAdminBundle\Field\ChoiceField; use EasyCorp\Bundle\EasyAdminBundle\Translation\TranslatableChoiceMessage; use EasyCorp\Bundle\EasyAdminBundle\Translation\TranslatableChoiceMessageCollection; +use Symfony\Component\Form\Extension\Core\Type\EnumType; use function Symfony\Component\String\u; use function Symfony\Component\Translation\t; use Symfony\Contracts\Translation\TranslatableInterface; @@ -38,7 +39,8 @@ public function configure(FieldDto $field, EntityDto $entityDto, AdminContext $c // in that case, get all the possible values of the Enum if (null === $choices && $enumsAreSupported) { $enumTypeClass = $field->getDoctrineMetadata()->get('enumType'); - if (enum_exists($enumTypeClass)) { + if (is_string($enumTypeClass) && enum_exists($enumTypeClass)) { + $field->setFormTypeOption('class', $enumTypeClass); $choices = $enumTypeClass::cases(); } } @@ -55,13 +57,11 @@ public function configure(FieldDto $field, EntityDto $entityDto, AdminContext $c $allChoicesAreEnums = false === \in_array(false, $elementIsEnum, true); if ($allChoicesAreEnums) { + $field->setFormType(EnumType::class); + $processedEnumChoices = []; foreach ($choices as $choice) { - if ($choice instanceof \BackedEnum) { - $processedEnumChoices[$choice->name] = $choice->value; - } else { - $processedEnumChoices[$choice->name] = $choice->name; - } + $processedEnumChoices[$choice->name] = $choice; } $choices = $processedEnumChoices; @@ -114,6 +114,13 @@ public function configure(FieldDto $field, EntityDto $entityDto, AdminContext $c if ($selectedValue instanceof TranslatableInterface) { $choiceMessage = $selectedValue; } else { + if (\is_object($selectedLabel)) { + $labeLCallback = $field->getFormTypeOption('choice_label'); + if (\is_callable($labeLCallback)) { + $selectedLabel = $labeLCallback($selectedLabel); + } + } + $choiceMessage = t( $selectedLabel, $translationParameters, From 2f05e5997adb44b0695a16b09f783634c44051b8 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Mon, 1 May 2023 15:26:45 +0200 Subject: [PATCH 2/3] - --- src/Factory/FieldFactory.php | 3 ++- src/Field/Configurator/ChoiceConfigurator.php | 8 ++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Factory/FieldFactory.php b/src/Factory/FieldFactory.php index 588b3e9fed..3bfbae2d40 100644 --- a/src/Factory/FieldFactory.php +++ b/src/Factory/FieldFactory.php @@ -107,7 +107,8 @@ public function processFields(EntityDto $entityDto, FieldCollection $fields): vo $configurator->configure($fieldDto, $entityDto, $context); } - + + // check again if the field is displayed because this can change in the configurators if (null !== $currentPage && false === $fieldDto->isDisplayedOn($currentPage)) { $fields->unset($fieldDto); diff --git a/src/Field/Configurator/ChoiceConfigurator.php b/src/Field/Configurator/ChoiceConfigurator.php index d42040780c..6bbb6e17b6 100644 --- a/src/Field/Configurator/ChoiceConfigurator.php +++ b/src/Field/Configurator/ChoiceConfigurator.php @@ -115,12 +115,12 @@ public function configure(FieldDto $field, EntityDto $entityDto, AdminContext $c $choiceMessage = $selectedValue; } else { if (\is_object($selectedLabel)) { - $labeLCallback = $field->getFormTypeOption('choice_label'); - if (\is_callable($labeLCallback)) { - $selectedLabel = $labeLCallback($selectedLabel); + $labelCallback = $field->getFormTypeOption('choice_label'); + if (\is_callable($labelCallback)) { + $selectedLabel = $labelCallback($selectedLabel); } } - + $choiceMessage = t( $selectedLabel, $translationParameters, From 4c44bde8048814e660a0343613f7990408e713d9 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Mon, 1 May 2023 15:57:36 +0200 Subject: [PATCH 3/3] - --- composer.json | 1 + src/Field/Configurator/ChoiceConfigurator.php | 17 ++++++++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 092467b8aa..4bbc7de5a7 100644 --- a/composer.json +++ b/composer.json @@ -29,6 +29,7 @@ "symfony/http-foundation": "^5.4|^6.0", "symfony/http-kernel": "^5.4|^6.0", "symfony/intl": "^5.4|^6.0", + "symfony/polyfill-php81": "^1.27", "symfony/property-access": "^5.4|^6.0", "symfony/security-bundle": "^5.4|^6.0", "symfony/string": "^5.4|^6.0", diff --git a/src/Field/Configurator/ChoiceConfigurator.php b/src/Field/Configurator/ChoiceConfigurator.php index 6bbb6e17b6..6e67e77bfd 100644 --- a/src/Field/Configurator/ChoiceConfigurator.php +++ b/src/Field/Configurator/ChoiceConfigurator.php @@ -50,6 +50,7 @@ public function configure(FieldDto $field, EntityDto $entityDto, AdminContext $c } // support for enums + $allChoicesAreEnums = false; if ($enumsAreSupported) { $elementIsEnum = array_unique(array_map(static function ($element): bool { return \is_object($element) && enum_exists($element::class); @@ -70,7 +71,15 @@ public function configure(FieldDto $field, EntityDto $entityDto, AdminContext $c if ($areChoicesTranslatable) { $field->setFormTypeOptionIfNotSet('choices', array_keys($choices)); - $field->setFormTypeOptionIfNotSet('choice_label', fn ($value) => $choices[$value]); + $field->setFormTypeOptionIfNotSet('choice_label', static function ($value) use ($choices) { + $key = match (true) { + \is_object($value) && enum_exists($value::class) => $value->name, + \is_object($value) => (string) $value, + default => $value, + }; + + return $choices[$key]; + }); } else { $field->setFormTypeOptionIfNotSet('choices', $choices); } @@ -108,13 +117,15 @@ public function configure(FieldDto $field, EntityDto $entityDto, AdminContext $c $translationDomain = $context->getI18n()->getTranslationDomain(); $choiceMessages = []; // Translatable choice don't need to get flipped - $flippedChoices = $areChoicesTranslatable ? $choices : array_flip($this->flatten($choices)); + $flippedChoices = ($areChoicesTranslatable || $allChoicesAreEnums) ? $choices : array_flip($this->flatten($choices)); foreach ((array) $fieldValue as $selectedValue) { if (null !== $selectedLabel = $flippedChoices[$selectedValue] ?? null) { if ($selectedValue instanceof TranslatableInterface) { $choiceMessage = $selectedValue; } else { - if (\is_object($selectedLabel)) { + if (\is_object($selectedLabel) && enum_exists($selectedLabel::class)) { + $selectedLabel = $selectedLabel->name; + } elseif (\is_object($selectedLabel)) { $labelCallback = $field->getFormTypeOption('choice_label'); if (\is_callable($labelCallback)) { $selectedLabel = $labelCallback($selectedLabel);