<?php
namespace App\Form\Type;
use Sylius\Bundle\ResourceBundle\Form\Registry\FormTypeRegistryInterface;
use Sylius\Bundle\ResourceBundle\Form\Type\AbstractResourceType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
final class SiteMenuFooterItemType extends AbstractResourceType
{
/**
* @var FormTypeRegistryInterface
*/
private $formTypeRegistry;
/**
* {@inheritdoc}
*/
public function __construct($dataClass, array $validationGroups = [], FormTypeRegistryInterface $formTypeRegistry)
{
parent::__construct($dataClass, $validationGroups);
$this->formTypeRegistry = $formTypeRegistry;
}
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options = [])
{
parent::buildForm($builder, $options);
$builder
->add('title', TextType::class, [
'label' => 'Titre de la navigation',
'required' => false
])
->add('attr_title', TextType::class, [
'label' => 'Attribut de titre',
'required' => false
])
->add('position', IntegerType::class, [
'label' => 'Position',
'required' => false,
'empty_data' => '0'
])
->add('type', SiteMenuFooterItemChoiceType::class, [
'label' => 'Type',
'attr' => [
'data-form-collection' => 'update',
],
])
->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($options) {
$type = $this->getRegistryIdentifier($event->getForm(), $event->getData());
if (null === $type) {
return;
}
$this->addConfigurationFields($event->getForm(), $this->formTypeRegistry->get($type, 'default'));
})
->addEventListener(FormEvents::POST_SET_DATA, function (FormEvent $event) {
$type = $this->getRegistryIdentifier($event->getForm(), $event->getData());
if (null === $type) {
return;
}
$event->getForm()->get('type')->setData($type);
})
->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) use ($options) {
$data = $event->getData();
if (!isset($data['type'])) {
return;
}
$this->addConfigurationFields($event->getForm(), $this->formTypeRegistry->get($data['type'], 'default'));
})
;
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver): void
{
parent::configureOptions($resolver);
$resolver
->setDefault('configuration_type', null)
->setAllowedTypes('configuration_type', ['string', 'null'])
;
}
/**
* @param FormInterface $form
* @param string $configurationType
*/
protected function addConfigurationFields(FormInterface $form, $configurationType)
{
$form->add('configuration', $configurationType, [
'label' => false,
]);
}
/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'app_site_menu_footer_item_type';
}
/**
* {@inheritdoc}
*/
protected function getRegistryIdentifier(FormInterface $form, $data = null)
{
if (null !== $data && null !== $data->getType()) {
return $data->getType();
}
if (null !== $form->getConfig()->hasOption('configuration_type')) {
return $form->getConfig()->getOption('configuration_type');
}
return null;
}
}