From fc76bd0f6d5d6d883f29c65295f29da38ba72c19 Mon Sep 17 00:00:00 2001 From: BA-JBI <31063297+BA-JBI@users.noreply.github.com> Date: Thu, 15 Aug 2024 09:23:54 +0200 Subject: [PATCH] Fix loading new item list after adding new created element on sortable collections #8201 Apply fix as proposed in https://github.com/sonata-project/SonataAdminBundle/issues/8201#issuecomment-2290796490 --- src/Form/Extension/ChoiceTypeExtension.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/Form/Extension/ChoiceTypeExtension.php b/src/Form/Extension/ChoiceTypeExtension.php index 836a12e005..45c688083e 100644 --- a/src/Form/Extension/ChoiceTypeExtension.php +++ b/src/Form/Extension/ChoiceTypeExtension.php @@ -27,6 +27,25 @@ */ final class ChoiceTypeExtension extends AbstractTypeExtension { + public function buildForm(FormBuilderInterface $builder, array $options): void + { + if ($options['multiple'] && (true === ($options['sortable'] ?? false))) { + $builder->addEventListener(FormEvents::PRE_SUBMIT, static function (FormEvent $event) use ($options) { + /** @var PreSubmitEvent $event */ + $form = $event->getForm(); + $data = $event->getData(); + + if (!is_array($data) || count($data) !== 1) { + return; + } + + if (str_contains($data[0], ',')) { + $event->setData(explode(',', $data[0])); + } + },1); + } + } + public function configureOptions(OptionsResolver $resolver): void { $optionalOptions = ['sortable'];