<?php
declare(strict_types=1);
namespace App\Form\Type;
use App\Entity\Customer\Customer;
use Sylius\Bundle\CoreBundle\Form\Type\ImageType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
final class CustomerPhotoType extends ImageType
{
public function __construct(string $dataClass, array $validationGroups = [])
{
parent::__construct($dataClass, $validationGroups);
}
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->remove('type')
->add('file', FileType::class, ['label' => 'sylius.form.image.file'])
->addEventListener(FormEvents::PRE_SET_DATA, [$this, 'onPreSetData']);
}
public function onPreSetData(FormEvent $event): void
{
$data = $event->getData();
if (! ($data instanceof Customer)) {
return;
}
$event->setData($data->getImage());
}
public function getBlockPrefix(): string
{
return 'app_customer_photo_type';
}
}