Skip to content
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

Fix ChoiceField Enum support and control field visibility inside Configurator #5640

Closed
wants to merge 12 commits into from
6 changes: 6 additions & 0 deletions src/Factory/FieldFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ public function processFields(EntityDto $entityDto, FieldCollection $fields): vo

$configurator->configure($fieldDto, $entityDto, $context);
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this code really needed? We have the same code a few lines above.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's needed because you need to control the display option inside the configurator.

if (null !== $currentPage && false === $fieldDto->isDisplayedOn($currentPage)) {
$fields->unset($fieldDto);

continue;
}

foreach ($fieldDto->getFormThemes() as $formThemePath) {
$context?->getCrud()?->addFormTheme($formThemePath);
Expand Down
19 changes: 13 additions & 6 deletions src/Field/Configurator/ChoiceConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}
Expand All @@ -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;
Expand Down Expand Up @@ -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,
Expand Down