Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2cd317d

Browse files
committed
bug EasyCorp#3194 Fixed some issues in SelectField (javiereguiluz)
This PR was squashed before being merged into the 3.0.x-dev branch. Discussion ---------- Fixed some issues in SelectField Fixes EasyCorp#3185. Commits ------- 66d7e9b Fixed some issues in SelectField
2 parents 9126366 + 66d7e9b commit 2cd317d

2 files changed

Lines changed: 36 additions & 6 deletions

File tree

src/Field/Configurator/SelectConfigurator.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,29 @@ public function configure(FieldDto $field, EntityDto $entityDto, AdminContext $c
3535

3636
$translatedChoices = [];
3737
$translationParameters = $context->getI18n()->getTranslationParameters();
38-
foreach ($choices as $key => $value) {
39-
$translatedKey = $this->translator->trans($key, $translationParameters);
40-
$translatedChoices[$translatedKey] = $value;
38+
foreach ($choices as $choiceLabel => $choiceValue) {
39+
$translatedChoiceLabel = $this->translator->trans((string) $choiceLabel, $translationParameters);
40+
$translatedChoices[$translatedChoiceLabel] = $choiceValue;
4141
}
4242
$field->setFormTypeOptionIfNotSet('choices', $translatedChoices);
4343

4444
if (null !== $value = $field->getValue()) {
45-
$selectedChoice = array_flip($choices)[$value];
46-
$field->setFormattedValue($this->translator->trans($selectedChoice, $translationParameters));
45+
// needed to be compatible with fields that allow selecting multiple values
46+
$selectedChoices = [];
47+
$flippedChoices = array_flip($choices);
48+
// $value is a scalar for single selections and an array for multiple selections
49+
foreach (array_values((array) $value) as $selectedValue) {
50+
if (null !== $selectedChoice = $flippedChoices[$selectedValue] ?? null) {
51+
$selectedChoices[] = $this->translator->trans($selectedChoice, $translationParameters);
52+
}
53+
}
54+
55+
$field->setFormattedValue(implode(', ', $selectedChoices));
4756
}
4857

58+
$field->setFormTypeOptionIfNotSet('multiple', $field->getCustomOption(SelectField::OPTION_ALLOW_MULTIPLE_SELECT));
59+
$field->setFormTypeOptionIfNotSet('expanded', $field->getCustomOption(SelectField::OPTION_RENDER_EXPANDED));
60+
4961
if (true === $field->getCustomOption(SelectField::OPTION_AUTOCOMPLETE)) {
5062
$field->setFormTypeOptionIfNotSet('attr.data-widget', 'select2');
5163
}

src/Field/SelectField.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ final class SelectField implements FieldInterface
1212
{
1313
use FieldTrait;
1414

15+
public const OPTION_ALLOW_MULTIPLE_SELECT = 'allowMultipleSelect';
1516
public const OPTION_AUTOCOMPLETE = 'autocomplete';
1617
public const OPTION_CHOICES = 'choices';
18+
public const OPTION_RENDER_EXPANDED = 'renderExpanded';
1719

1820
public static function new(string $propertyName, ?string $label = null): self
1921
{
@@ -23,7 +25,16 @@ public static function new(string $propertyName, ?string $label = null): self
2325
->setTemplateName('crud/field/select')
2426
->setFormType(ChoiceType::class)
2527
->addCssClass('field-select')
26-
->setCustomOption(self::OPTION_CHOICES, null);
28+
->setCustomOption(self::OPTION_ALLOW_MULTIPLE_SELECT, false)
29+
->setCustomOption(self::OPTION_CHOICES, null)
30+
->setCustomOption(self::OPTION_RENDER_EXPANDED, false);
31+
}
32+
33+
public function allowMultipleSelect(bool $allow = true): self
34+
{
35+
$this->setCustomOption(self::OPTION_ALLOW_MULTIPLE_SELECT, $allow);
36+
37+
return $this;
2738
}
2839

2940
public function autocomplete(): self
@@ -43,4 +54,11 @@ public function setChoices(array $keyValueChoices): self
4354

4455
return $this;
4556
}
57+
58+
public function renderExpanded(bool $expanded = true): self
59+
{
60+
$this->setCustomOption(self::OPTION_RENDER_EXPANDED, $expanded);
61+
62+
return $this;
63+
}
4664
}

0 commit comments

Comments
 (0)