<?php
namespace App\Form\Type\EventListener;
use App\Entity\Core\Product;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
class SetDefaultProductDataListener implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
FormEvents::PRE_SUBMIT => 'onPreSubmit',
];
}
public function onPreSubmit(FormEvent $event)
{
$product = $event->getData();
if (!$product) {
return;
}
$this->setDefaultData($event);
}
private function setDefaultData(FormEvent $event): void
{
$product = $event->getData();
$product['variantSelectionMethod'] = Product::DEFAULT_VARIANT_SELECTION;
$event->setData($product);
}
}