src/Form/ProductAttribute/Subscriber/ProductAttributeSubscriber.php line 31

Open in your IDE?
  1. <?php
  2. namespace App\Form\ProductAttribute\Subscriber;
  3. use App\Form\ProductAttribute\ProductAttributeValueType;
  4. use App\Repository\ProductAttribute\ProductAttributeRepository;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\Form\Exception\UnexpectedTypeException;
  9. use Symfony\Component\Form\FormEvent;
  10. use Symfony\Component\Form\FormEvents;
  11. use Symfony\Component\Form\FormInterface;
  12. class ProductAttributeSubscriber implements EventSubscriberInterface
  13. {
  14.     public function __construct(private readonly ProductAttributeRepository $productAttributeRepository)
  15.     {
  16.     }
  17.     public static function getSubscribedEvents(): array
  18.     {
  19.         return [
  20.             FormEvents::PRE_SET_DATA => 'preSetData',
  21.             FormEvents::POST_SET_DATA => 'postSetData',
  22.             FormEvents::PRE_SUBMIT => 'preSubmit',
  23.             FormEvents::SUBMIT => ['onSubmit'50],
  24.         ];
  25.     }
  26.     public function preSetData(FormEvent $event)
  27.     {
  28.         $form $event->getForm();
  29.         $data $event->getData();
  30.         if (!$data) {
  31.             $data = new ArrayCollection();
  32.         }
  33.         if (!$data instanceof Collection) {
  34.             throw new UnexpectedTypeException($dataCollection::class);
  35.         }
  36.         // First remove all rows
  37.         foreach ($form as $name => $child) {
  38.             $form->remove($name);
  39.         }
  40.         foreach ($data as $name => $value) {
  41.             $form->add($nameProductAttributeValueType::class, [
  42.                 'product_attribute' => $value->getProductAttribute(),
  43.             ]);
  44.         }
  45.     }
  46.     public function postSetData(FormEvent $event)
  47.     {
  48.     }
  49.     public function preSubmit(FormEvent $event)
  50.     {
  51.         $form $event->getForm();
  52.         $data $event->getData();
  53.         if (!\is_array($data)) {
  54.             $data = [];
  55.         }
  56.         // Remove all empty rows
  57.         foreach ($form as $name => $child) {
  58.             if (!isset($data[$name])) {
  59.                 $form->remove($name);
  60.             }
  61.         }
  62.         // Add all additional rows
  63.         foreach ($data as $name => $value) {
  64.             if (!$form->has($name)) {
  65.                 $form->add($nameProductAttributeValueType::class, [
  66.                     'product_attribute' => $this->productAttributeRepository->find($value['productAttribute']),
  67.                 ]);
  68.             }
  69.         }
  70.         $event->setData($data);
  71.     }
  72.     public function onSubmit(FormEvent $event)
  73.     {
  74.         $form $event->getForm();
  75.         $data $event->getData();
  76.         // At this point, $data is an array or an array-like object that already contains the
  77.         // new entries, which were added by the data mapper. The data mapper ignores existing
  78.         // entries, so we need to manually unset removed entries in the collection.
  79.         if (null === $data) {
  80.             $data = new ArrayCollection();
  81.         }
  82.         if (is_array($data)) {
  83.             $data = new ArrayCollection($data);
  84.         }
  85.         if (!$data instanceof Collection) {
  86.             throw new UnexpectedTypeException($dataCollection::class);
  87.         }
  88.         /** @var FormInterface $child */
  89.         foreach ($form as $name => $child) {
  90.             if ($child->isEmpty()) {
  91.                 unset($data[$name]);
  92.                 $form->remove($name);
  93.             }
  94.         }
  95.         $toDelete = [];
  96.         foreach ($data as $name => $child) {
  97.             if (!$form->has($name)) {
  98.                 $toDelete[] = $name;
  99.             }
  100.         }
  101.         foreach ($toDelete as $name) {
  102.             unset($data[$name]);
  103.         }
  104.     }
  105. }