-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #202 from sherlockode/fix/repeater_position
Fix repeater position for non compound children
- Loading branch information
Showing
2 changed files
with
82 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace Sherlockode\AdvancedContentBundle\Form\Type; | ||
|
||
use Symfony\Component\Form\AbstractType; | ||
use Symfony\Component\Form\Extension\Core\Type\HiddenType; | ||
use Symfony\Component\Form\FormBuilderInterface; | ||
use Symfony\Component\Form\FormEvent; | ||
use Symfony\Component\Form\FormEvents; | ||
use Symfony\Component\OptionsResolver\Options; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
||
/** | ||
* FormType wrapper for non-compound forms inside RepeaterType | ||
* Used in order to be able to add the "position" field | ||
*/ | ||
class RepeatedChildWrappedType extends AbstractType | ||
{ | ||
public function buildForm(FormBuilderInterface $builder, array $options) | ||
{ | ||
$builder | ||
->add('wrapped_child', $options['child_form'], $options['child_options']) | ||
->add('position', HiddenType::class, ['data' => $options['position']]) | ||
; | ||
|
||
$dataCallback = function (FormEvent $event) { | ||
$data = $event->getData(); | ||
|
||
$data = ['wrapped_child' => $data]; | ||
|
||
$event->setData($data); | ||
}; | ||
|
||
$builder->addEventListener(FormEvents::PRE_SET_DATA, $dataCallback, -5); | ||
} | ||
|
||
public function configureOptions(OptionsResolver $resolver) | ||
{ | ||
$resolver->setDefaults([ | ||
'position' => 0, | ||
'child_options'=> [], | ||
]); | ||
$resolver->setRequired(['child_form']); | ||
$resolver->setNormalizer('child_options', function (Options $options, $value) { | ||
unset($value['property_path']); | ||
|
||
return $value; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters