src/Aqarmap/Bundle/ListingBundle/EventListener/BuildListingAttributeFormListener.php line 41

Open in your IDE?
  1. <?php
  2. namespace Aqarmap\Bundle\ListingBundle\EventListener;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  5. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  6. use Symfony\Component\Form\Extension\Core\Type\IntegerType;
  7. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  8. use Symfony\Component\Form\Extension\Core\Type\TextType;
  9. use Symfony\Component\Form\FormEvent;
  10. use Symfony\Component\Form\FormEvents;
  11. use Symfony\Component\Form\FormFactoryInterface;
  12. use Symfony\Component\Validator\Constraints\NotBlank;
  13. class BuildListingAttributeFormListener implements EventSubscriberInterface
  14. {
  15.     /**
  16.      * Form factory.
  17.      *
  18.      * @var FormFactoryInterface
  19.      */
  20.     private $factory;
  21.     /**
  22.      * Constructor.
  23.      */
  24.     public function __construct(FormFactoryInterface $factory)
  25.     {
  26.         $this->factory $factory;
  27.     }
  28.     public static function getSubscribedEvents()
  29.     {
  30.         return [FormEvents::PRE_SET_DATA => 'buildForm'];
  31.     }
  32.     /**
  33.      * Builds proper listing form after setting the listing.
  34.      */
  35.     public function buildForm(FormEvent $event): void
  36.     {
  37.         $data $event->getData();
  38.         $form $event->getForm();
  39.         if (null === $data) {
  40.             $form->add($this->factory->createNamed('value'TextType::class, null, ['auto_initialize' => false]));
  41.             return;
  42.         }
  43.         // Build field options
  44.         $options = ['label' => $data->getCustomField()->getLabel(), 'auto_initialize' => false];
  45.         if (\is_array($data->getCustomField()->getOptions())) {
  46.             $options array_merge($options$data->getCustomField()->getOptions());
  47.             if ('integer' === $data->getCustomField()->getType()) {
  48.                 unset($options['attr']['choices']);
  49.             }
  50.             $options['attr']['required'] = $data->getCustomField()->getRequired() && !$data->getListing()->hasParent() ? true false;
  51.             $options['attr']['inputmode'] = 'integer' === $data->getCustomField()->getType() ? 'numeric' '';
  52.             // Add field constraints
  53.             $constraints = [];
  54.             if ($data->getCustomField()->getRequired() && !$data->getListing()->hasParent()) {
  55.                 $constraints[] = new NotBlank();
  56.             }
  57.             $options array_merge($options, ['constraints' => $constraints]);
  58.             if ('choice' == $data->getCustomField()->getType()) {
  59.                 $options['choices'] = array_flip($options['choices']);
  60.                 if (!$data->getCustomField()->getRequired()) {
  61.                     $options['placeholder'] = $data->getCustomField()->getLabel();
  62.                 }
  63.             }
  64.         }
  65.         $inputTypes = [
  66.             'integer' => IntegerType::class,
  67.             'text' => TextType::class,
  68.             'choice' => ChoiceType::class,
  69.             'textarea' => TextareaType::class,
  70.             'hidden' => HiddenType::class,
  71.         ];
  72.         $formType $this->factory
  73.             ->createNamedBuilder('value'$inputTypes[$data->getCustomField()->getType()], null$options);
  74.         if ('integer' === $data->getCustomField()->getType()) {
  75.             $formType->resetViewTransformers();
  76.         }
  77.         $form
  78.             ->remove('custom_field')
  79.             ->add($formType->getForm())
  80.         ;
  81.     }
  82. }