From 58d29259578070bbd9ef28992c90ad0668eacd20 Mon Sep 17 00:00:00 2001 From: Pascal CESCON - Amoifr Date: Mon, 30 Mar 2026 09:28:42 +0200 Subject: [PATCH 1/2] Add choice_label support to CrudAutocompleteType When using EntityFilter with autocomplete() and a custom choice_label, the form crashed because CrudAutocompleteType did not define choice_label as a valid option. Changes: - Add choice_label option to CrudAutocompleteType - Preserve user-defined choice_label in CrudAutocompleteSubscriber Fix #7462 --- .../CrudAutocompleteSubscriber.php | 24 +++++++++++-------- src/Form/Type/CrudAutocompleteType.php | 3 +++ 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/Form/EventListener/CrudAutocompleteSubscriber.php b/src/Form/EventListener/CrudAutocompleteSubscriber.php index 5e922907c2..e96d464a7e 100644 --- a/src/Form/EventListener/CrudAutocompleteSubscriber.php +++ b/src/Form/EventListener/CrudAutocompleteSubscriber.php @@ -48,16 +48,20 @@ public function preSetData(FormEvent $event) // the renderAsHtml flag controls how TomSelect renders the item (via data-ea-autocomplete-render-items-as-html). $callback = $options['autocomplete_callback'] ?? null; $template = $options['autocomplete_template'] ?? null; - - if (null !== $template) { - $twig = $this->twig; - $options['choice_label'] = static function ($entity) use ($twig, $template): string { - return $twig->render($template, ['entity' => $entity]); - }; - } elseif (null !== $callback) { - $options['choice_label'] = static function ($entity) use ($callback): string { - return (string) $callback($entity); - }; + $choiceLabel = $options['choice_label'] ?? null; + + // only generate choice_label from template/callback if not already set by the user + if (null === $choiceLabel) { + if (null !== $template) { + $twig = $this->twig; + $options['choice_label'] = static function ($entity) use ($twig, $template): string { + return $twig->render($template, ['entity' => $entity]); + }; + } elseif (null !== $callback) { + $options['choice_label'] = static function ($entity) use ($callback): string { + return (string) $callback($entity); + }; + } } // remove custom options before passing to EntityType diff --git a/src/Form/Type/CrudAutocompleteType.php b/src/Form/Type/CrudAutocompleteType.php index ed49b3f65e..9df8e801ee 100644 --- a/src/Form/Type/CrudAutocompleteType.php +++ b/src/Form/Type/CrudAutocompleteType.php @@ -48,11 +48,14 @@ public function configureOptions(OptionsResolver $resolver): void // options for custom rendering of selected items (to match the rendering of the other entries in the dropdown) 'autocomplete_callback' => null, 'autocomplete_template' => null, + // allow choice_label to customize how selected items are displayed + 'choice_label' => null, ]); $resolver->setRequired(['class']); $resolver->setAllowedTypes('autocomplete_callback', ['null', 'callable']); $resolver->setAllowedTypes('autocomplete_template', ['null', 'string']); + $resolver->setAllowedTypes('choice_label', ['null', 'string', 'callable', 'bool']); } public function getBlockPrefix(): string From 945cd4994768a2f736be13215ff3c87985b68a41 Mon Sep 17 00:00:00 2001 From: Pascal CESCON - Amoifr Date: Mon, 30 Mar 2026 09:28:42 +0200 Subject: [PATCH 2/2] Fix choice_label compatibility with Symfony < 8.1 Don't pass null choice_label explicitly to EntityType as it behaves differently than omitting the option in older Symfony versions. --- src/Form/EventListener/CrudAutocompleteSubscriber.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Form/EventListener/CrudAutocompleteSubscriber.php b/src/Form/EventListener/CrudAutocompleteSubscriber.php index e96d464a7e..3b2bf6b9e0 100644 --- a/src/Form/EventListener/CrudAutocompleteSubscriber.php +++ b/src/Form/EventListener/CrudAutocompleteSubscriber.php @@ -70,6 +70,12 @@ public function preSetData(FormEvent $event) $options['autocomplete_template'] ); + // Don't pass null choice_label to EntityType - let it use __toString() default + // (passing null explicitly behaves differently than omitting the option in Symfony < 8.1) + if (null === ($options['choice_label'] ?? null)) { + unset($options['choice_label']); + } + $form->add('autocomplete', EntityType::class, $options); }