src/Form/EventListener/AddEstadoFieldSubscriber.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Form\EventListener;
  3. use App\Entity\Reports;
  4. use Symfony\Component\Form\FormEvent;
  5. use Symfony\Component\Form\FormEvents;
  6. use Symfony\Component\Form\FormFactoryInterface;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Doctrine\ORM\EntityRepository;
  9. use Symfony\Component\PropertyAccess\PropertyAccess;
  10. class AddEstadoFieldSubscriber implements EventSubscriberInterface
  11. {
  12.    /* private $factory;
  13.     public function __construct(FormFactoryInterface $factory)
  14.     {
  15.         $this->factory = $factory;
  16.     }*/
  17.     public static function getSubscribedEvents()
  18.     {
  19.         return [
  20.             FormEvents::PRE_SET_DATA => 'onPreSetData',
  21.             FormEvents::PRE_SUBMIT   => 'onPreSubmit',
  22.         ];
  23.     }
  24.     private function addEstadoForm($form$estado)
  25.     {
  26.         $form->add($this->factory->createNamed('estado_siniestro''entity'$estado, array(
  27.             'class'         => Reports::class,
  28.             'property' => 'estado',
  29.             'mapped'        => false,
  30.             'empty_value'   => 'Estado',
  31.             'auto_initialize' => false,
  32.             'query_builder' => function (EntityRepository $repository) {
  33.                 $qb $repository->createQueryBuilder('s')
  34.                 ->groupBy('s.estado');
  35.                 return $qb;
  36.             }
  37.         )));
  38.     }
  39.     public function preSetData(FormEvent $event)
  40.     {
  41.         $data $event->getData();
  42.         $form $event->getForm();
  43.         if (null === $data) {
  44.             return;
  45.         }
  46.         $estado = ($data->getSiniestroId()) ? $data->getSiniestroId()->getEstado() : null ;
  47.         $this->addEstadoForm($form$estado);
  48.     }
  49.     public function preBind(FormEvent $event)
  50.     {
  51.         $data $event->getData();
  52.         $form $event->getForm();
  53.         if (null === $data) {
  54.             return;
  55.         }
  56.         $estado array_key_exists('siniestroId'$data) ? $data['siniestroId'] : null;
  57.         $this->addEstadoForm($form$estado);
  58.     }
  59. }