|
2 | 2 |
|
3 | 3 | namespace EasyCorp\Bundle\EasyAdminBundle\Form\EventListener; |
4 | 4 |
|
5 | | -use Doctrine\ORM\Mapping\FieldMapping; |
6 | 5 | use Symfony\Bridge\Doctrine\Form\Type\EntityType; |
7 | 6 | use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
8 | 7 | use Symfony\Component\Form\FormEvent; |
@@ -38,39 +37,31 @@ public function preSetData(FormEvent $event): void |
38 | 37 | $options['compound'] = false; |
39 | 38 | $options['choices'] = is_iterable($data) ? $data : [$data]; |
40 | 39 |
|
41 | | - // apply custom choice_label if autocomplete is customized so the selected item looks the same as the other entries. |
42 | | - // note: we don't escape here because Twig already escapes the <option> content automatically; |
43 | | - // the renderAsHtml flag controls how TomSelect renders the item (via data-ea-autocomplete-render-items-as-html). |
| 40 | + // strip EA-specific options before forwarding to EntityType |
44 | 41 | $callback = $options['autocomplete_callback'] ?? null; |
45 | 42 | $template = $options['autocomplete_template'] ?? null; |
46 | | - $choiceLabel = $options['choice_label'] ?? null; |
47 | | - |
48 | | - // only generate choice_label from template/callback if not already set by the user |
49 | | - if (null === $choiceLabel) { |
| 43 | + unset($options['autocomplete_callback'], $options['autocomplete_template']); |
| 44 | + |
| 45 | + // Resolve choice_label: |
| 46 | + // - if the user supplied one (via value_type_options.choice_label), keep it; |
| 47 | + // - otherwise derive one from autocomplete_template / autocomplete_callback so the |
| 48 | + // selected item matches the rendering of other entries in the dropdown; |
| 49 | + // - otherwise drop the option entirely so EntityType falls back to __toString(). |
| 50 | + // |
| 51 | + // Note: we don't escape here because Twig already escapes the <option> content |
| 52 | + // automatically; the renderAsHtml flag controls how TomSelect renders the item |
| 53 | + // (via data-ea-autocomplete-render-items-as-html). |
| 54 | + if (null === ($options['choice_label'] ?? null)) { |
50 | 55 | if (null !== $template) { |
51 | 56 | $twig = $this->twig; |
52 | | - $options['choice_label'] = static function ($entity) use ($twig, $template): string { |
53 | | - return $twig->render($template, ['entity' => $entity]); |
54 | | - }; |
| 57 | + $options['choice_label'] = static fn ($entity): string => $twig->render($template, ['entity' => $entity]); |
55 | 58 | } elseif (null !== $callback) { |
56 | | - $options['choice_label'] = static function ($entity) use ($callback): string { |
57 | | - return (string) $callback($entity); |
58 | | - }; |
| 59 | + $options['choice_label'] = static fn ($entity): string => (string) $callback($entity); |
| 60 | + } else { |
| 61 | + unset($options['choice_label']); |
59 | 62 | } |
60 | 63 | } |
61 | 64 |
|
62 | | - // remove custom options before passing to EntityType |
63 | | - unset( |
64 | | - $options['autocomplete_callback'], |
65 | | - $options['autocomplete_template'] |
66 | | - ); |
67 | | - |
68 | | - // Don't pass null choice_label to EntityType - let it use __toString() default |
69 | | - // (passing null explicitly behaves differently than omitting the option in Symfony < 8.1) |
70 | | - if (null === ($options['choice_label'] ?? null)) { |
71 | | - unset($options['choice_label']); |
72 | | - } |
73 | | - |
74 | 65 | $form->add('autocomplete', EntityType::class, $options); |
75 | 66 | } |
76 | 67 |
|
|
0 commit comments