From ae0f56a5d3961b5eaca9b4d059399795c2575a0c Mon Sep 17 00:00:00 2001 From: Neil Peyssard Date: Tue, 16 May 2023 14:13:31 +0200 Subject: [PATCH] Fix data serialization for multiple choice types --- Form/Type/ElementAdvancedType.php | 13 +--------- Form/Type/ElementHideOnType.php | 36 ++++++++++++++++++++++++++ Resources/views/Form/content.html.twig | 2 +- Twig/Extension/ContentExtension.php | 19 ++++++++++---- 4 files changed, 52 insertions(+), 18 deletions(-) create mode 100644 Form/Type/ElementHideOnType.php diff --git a/Form/Type/ElementAdvancedType.php b/Form/Type/ElementAdvancedType.php index 23a9349..85b6c83 100644 --- a/Form/Type/ElementAdvancedType.php +++ b/Form/Type/ElementAdvancedType.php @@ -4,7 +4,6 @@ use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Core\Type\TextType; -use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\FormBuilderInterface; class ElementAdvancedType extends AbstractType @@ -20,19 +19,9 @@ public function buildForm(FormBuilderInterface $builder, array $options) 'label' => 'configuration.advanced.id', 'required' => false, ]) - ->add('hide_on', ChoiceType::class, [ + ->add('hide_on', ElementHideOnType::class, [ 'label' => 'configuration.advanced.hide_on', 'required' => false, - 'choices' => [ - 'responsive.devices.mobile_portrait' => 'xs', - 'responsive.devices.mobile_landscape' => 'sm', - 'responsive.devices.tablet_portrait' => 'md', - 'responsive.devices.tablet_landscape' => 'lg', - 'responsive.devices.desktop' => 'xl', - ], - 'multiple' => true, - 'expanded' => true, - 'block_prefix' => 'acb_advanced_hide_on', ]) ; } diff --git a/Form/Type/ElementHideOnType.php b/Form/Type/ElementHideOnType.php new file mode 100644 index 0000000..655cfbc --- /dev/null +++ b/Form/Type/ElementHideOnType.php @@ -0,0 +1,36 @@ +setDefaults([ + 'choices' => [ + 'responsive.devices.mobile_portrait' => 'xs', + 'responsive.devices.mobile_landscape' => 'sm', + 'responsive.devices.tablet_portrait' => 'md', + 'responsive.devices.tablet_landscape' => 'lg', + 'responsive.devices.desktop' => 'xl', + ], + 'multiple' => true, + 'expanded' => true, + ]); + } +} diff --git a/Resources/views/Form/content.html.twig b/Resources/views/Form/content.html.twig index 9e65890..427dac6 100644 --- a/Resources/views/Form/content.html.twig +++ b/Resources/views/Form/content.html.twig @@ -274,7 +274,7 @@ {% endblock %} -{% block acb_advanced_hide_on_widget %} +{% block element_hide_on_widget %}
{%- for child in form %} diff --git a/Twig/Extension/ContentExtension.php b/Twig/Extension/ContentExtension.php index 0650f2a..e40de6b 100644 --- a/Twig/Extension/ContentExtension.php +++ b/Twig/Extension/ContentExtension.php @@ -368,16 +368,25 @@ private function getPixelProperties(): array */ public function getJsonForm(FormView $form) { - $json = []; - if ($form->vars['compound'] && !in_array('acb_advanced_hide_on', $form->vars['block_prefixes'])) { + // Looping on multiple choice type children will return an array of all available choices, + // using the form value allows us to retrieve only the selected choices + $useValueForSerialization = ( + isset($form->vars['choices']) + && isset($form->vars['multiple']) + && true === $form->vars['multiple'] + ); + + if ($form->vars['compound'] && !$useValueForSerialization) { foreach ($form->children as $child) { $json[$child->vars['name']] = $this->getJsonForm($child); } - } else { - return in_array('acb_advanced_hide_on', $form->vars['block_prefixes']) ? $form->vars['value'] : $form->vars['data']; + + return $json ?? []; + } elseif ($useValueForSerialization || is_object($form->vars['data'])) { + return $form->vars['value']; } - return $json; + return $form->vars['data']; } /**