src/Form/Type/SiteMenuFooterItemType.php line 26

Open in your IDE?
  1. <?php
  2. namespace App\Form\Type;
  3. use Sylius\Bundle\ResourceBundle\Form\Registry\FormTypeRegistryInterface;
  4. use Sylius\Bundle\ResourceBundle\Form\Type\AbstractResourceType;
  5. use Symfony\Component\Form\Extension\Core\Type\IntegerType;
  6. use Symfony\Component\Form\Extension\Core\Type\TextType;
  7. use Symfony\Component\Form\FormBuilderInterface;
  8. use Symfony\Component\Form\FormEvent;
  9. use Symfony\Component\Form\FormEvents;
  10. use Symfony\Component\Form\FormInterface;
  11. use Symfony\Component\OptionsResolver\OptionsResolver;
  12. final class SiteMenuFooterItemType extends AbstractResourceType
  13. {
  14.     /**
  15.      * @var FormTypeRegistryInterface
  16.      */
  17.     private $formTypeRegistry;
  18.     
  19.     /**
  20.      * {@inheritdoc}
  21.      */
  22.     public function __construct($dataClass, array $validationGroups = [], FormTypeRegistryInterface $formTypeRegistry)
  23.     {
  24.         parent::__construct($dataClass$validationGroups);
  25.         $this->formTypeRegistry $formTypeRegistry;
  26.     }
  27.     /**
  28.      * {@inheritdoc}
  29.      */
  30.     public function buildForm(FormBuilderInterface $builder, array $options = [])
  31.     {
  32.         parent::buildForm($builder$options);
  33.         $builder
  34.             ->add('title'TextType::class, [
  35.                 'label' => 'Titre de la navigation',
  36.                 'required' => false
  37.             ])
  38.             ->add('attr_title'TextType::class, [
  39.                 'label' => 'Attribut de titre',
  40.                 'required' => false
  41.             ])
  42.             ->add('position'IntegerType::class, [
  43.                 'label' => 'Position',
  44.                 'required' => false,
  45.                 'empty_data' => '0'
  46.             ])
  47.             ->add('type'SiteMenuFooterItemChoiceType::class, [
  48.                 'label' => 'Type',
  49.                 'attr' => [
  50.                     'data-form-collection' => 'update',
  51.                 ],
  52.             ])
  53.             ->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($options) {
  54.                 $type $this->getRegistryIdentifier($event->getForm(), $event->getData());
  55.                 if (null === $type) {
  56.                     return;
  57.                 }
  58.                 $this->addConfigurationFields($event->getForm(), $this->formTypeRegistry->get($type'default'));
  59.             })
  60.             ->addEventListener(FormEvents::POST_SET_DATA, function (FormEvent $event) {
  61.                 $type $this->getRegistryIdentifier($event->getForm(), $event->getData());
  62.                 if (null === $type) {
  63.                     return;
  64.                 }
  65.                 $event->getForm()->get('type')->setData($type);
  66.             })
  67.             ->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) use ($options) {
  68.                 $data $event->getData();
  69.                 if (!isset($data['type'])) {
  70.                     return;
  71.                 }
  72.                 $this->addConfigurationFields($event->getForm(), $this->formTypeRegistry->get($data['type'], 'default'));
  73.             })
  74.         ;
  75.     }
  76.     /**
  77.      * {@inheritdoc}
  78.      */
  79.     public function configureOptions(OptionsResolver $resolver): void
  80.     {
  81.         parent::configureOptions($resolver);
  82.         $resolver
  83.             ->setDefault('configuration_type'null)
  84.             ->setAllowedTypes('configuration_type', ['string''null'])
  85.         ;
  86.     }
  87.     /**
  88.      * @param FormInterface $form
  89.      * @param string $configurationType
  90.      */
  91.     protected function addConfigurationFields(FormInterface $form$configurationType)
  92.     {
  93.         $form->add('configuration'$configurationType, [
  94.             'label' => false,
  95.         ]);
  96.     }
  97.     /**
  98.      * {@inheritdoc}
  99.      */
  100.     public function getBlockPrefix()
  101.     {
  102.         return 'app_site_menu_footer_item_type';
  103.     }
  104.     /**
  105.      * {@inheritdoc}
  106.      */
  107.     protected function getRegistryIdentifier(FormInterface $form$data null)
  108.     {
  109.         if (null !== $data && null !== $data->getType()) {
  110.             return $data->getType();
  111.         }
  112.         if (null !== $form->getConfig()->hasOption('configuration_type')) {
  113.             return $form->getConfig()->getOption('configuration_type');
  114.         }
  115.         return null;
  116.     }
  117. }