From c7a00161a0dfb31f4d3cf31e4efdeeae9dfcc559 Mon Sep 17 00:00:00 2001 From: BA-JBI <31063297+BA-JBI@users.noreply.github.com> Date: Thu, 15 Aug 2024 09:21:38 +0200 Subject: [PATCH 1/5] Fix adding new created element on sortable collections #8201 Apply fix as proposed in https://github.com/sonata-project/SonataAdminBundle/issues/8201#issue-2465762116 --- .../CRUD/Association/edit_many_script.html.twig | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/Resources/views/CRUD/Association/edit_many_script.html.twig b/src/Resources/views/CRUD/Association/edit_many_script.html.twig index a23ab8070b..7825c44da0 100644 --- a/src/Resources/views/CRUD/Association/edit_many_script.html.twig +++ b/src/Resources/views/CRUD/Association/edit_many_script.html.twig @@ -329,17 +329,24 @@ This code manages the many-to-[one|many] association field popup data: {_xml_http_request: true }, dataType: 'html', type: 'POST', - success: function(html) { + success: function(html) { jQuery('#field_container_{{ id }}').replaceWith(html); var newElement = jQuery('#{{ id }} [value="' + data.objectId + '"]'); - if (newElement.is("input")) { - newElement.attr('checked', 'checked'); + + if (newElement.length) { + if (newElement.is("input")) { + newElement.attr('checked', 'checked'); + } else { + newElement.attr('selected', 'selected'); + } } else { - newElement.attr('selected', 'selected'); + var selections = jQuery('#{{ id }}').val().split(','); + selections.push(data.objectId); + jQuery('#{{ id }}').val(selections.filter((val) => val.length > 0).join(',')); } jQuery('#field_container_{{ id }}').trigger('sonata-admin-append-form-element'); - } + } }); {% endif %} 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 2/5] 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']; From 5a53c8907cbe489b0a7471b720f8eedb3a205831 Mon Sep 17 00:00:00 2001 From: BA-JBI <31063297+BA-JBI@users.noreply.github.com> Date: Thu, 15 Aug 2024 10:16:16 +0200 Subject: [PATCH 3/5] Add missing use statements --- src/Form/Extension/ChoiceTypeExtension.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Form/Extension/ChoiceTypeExtension.php b/src/Form/Extension/ChoiceTypeExtension.php index 45c688083e..2ae342d27c 100644 --- a/src/Form/Extension/ChoiceTypeExtension.php +++ b/src/Form/Extension/ChoiceTypeExtension.php @@ -15,6 +15,8 @@ use Symfony\Component\Form\AbstractTypeExtension; use Symfony\Component\Form\Extension\Core\Type\ChoiceType; +use Symfony\Component\Form\FormBuilderInterface; +use Symfony\Component\Form\FormEvents; use Symfony\Component\Form\FormInterface; use Symfony\Component\Form\FormTypeInterface; use Symfony\Component\Form\FormView; From 8da174bcdd471627b88666cf3742f0bfc4b4fadc Mon Sep 17 00:00:00 2001 From: BA-JBI <31063297+BA-JBI@users.noreply.github.com> Date: Thu, 15 Aug 2024 10:24:28 +0200 Subject: [PATCH 4/5] Add missing use statement --- src/Form/Extension/ChoiceTypeExtension.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Form/Extension/ChoiceTypeExtension.php b/src/Form/Extension/ChoiceTypeExtension.php index 2ae342d27c..f2f835f1fd 100644 --- a/src/Form/Extension/ChoiceTypeExtension.php +++ b/src/Form/Extension/ChoiceTypeExtension.php @@ -16,6 +16,7 @@ use Symfony\Component\Form\AbstractTypeExtension; use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\FormBuilderInterface; +use Symfony\Component\Form\FormEvent; use Symfony\Component\Form\FormEvents; use Symfony\Component\Form\FormInterface; use Symfony\Component\Form\FormTypeInterface; From 0ce9927514aab3396d006f50932493e50e4d8388 Mon Sep 17 00:00:00 2001 From: BA-JBI <31063297+BA-JBI@users.noreply.github.com> Date: Tue, 19 Nov 2024 13:29:29 +0100 Subject: [PATCH 5/5] Update src/Form/Extension/ChoiceTypeExtension.php Co-authored-by: Vincent Langlet --- src/Form/Extension/ChoiceTypeExtension.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Form/Extension/ChoiceTypeExtension.php b/src/Form/Extension/ChoiceTypeExtension.php index f2f835f1fd..8bf6439ba5 100644 --- a/src/Form/Extension/ChoiceTypeExtension.php +++ b/src/Form/Extension/ChoiceTypeExtension.php @@ -45,7 +45,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void if (str_contains($data[0], ',')) { $event->setData(explode(',', $data[0])); } - },1); + }, 1); } }