Skip to content

Commit

Permalink
Merge pull request #290 from sherlockode/fix/data_serialization
Browse files Browse the repository at this point in the history
Fix data serialization for multiple choice types
  • Loading branch information
Vowow authored May 16, 2023
2 parents 8ea9db5 + ae0f56a commit 77b136a
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 18 deletions.
13 changes: 1 addition & 12 deletions Form/Type/ElementAdvancedType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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',
])
;
}
Expand Down
36 changes: 36 additions & 0 deletions Form/Type/ElementHideOnType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Sherlockode\AdvancedContentBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\OptionsResolver\OptionsResolver;

class ElementHideOnType extends AbstractType
{
/**
* @return string
*/
public function getParent()
{
return ChoiceType::class;
}

/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->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,
]);
}
}
2 changes: 1 addition & 1 deletion Resources/views/Form/content.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@
</table>
{% endblock %}

{% block acb_advanced_hide_on_widget %}
{% block element_hide_on_widget %}
<div {{ block('widget_container_attributes') }}>
<div class="{% if required %}required {% endif %} acb-advanced-hide-on">
{%- for child in form %}
Expand Down
19 changes: 14 additions & 5 deletions Twig/Extension/ContentExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
}

/**
Expand Down

0 comments on commit 77b136a

Please sign in to comment.