src/Form/Type/CustomerPhotoType.php line 14

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Form\Type;
  4. use App\Entity\Customer\Customer;
  5. use Sylius\Bundle\CoreBundle\Form\Type\ImageType;
  6. use Symfony\Component\Form\Extension\Core\Type\FileType;
  7. use Symfony\Component\Form\FormBuilderInterface;
  8. use Symfony\Component\Form\FormEvent;
  9. use Symfony\Component\Form\FormEvents;
  10. final class CustomerPhotoType extends ImageType
  11. {
  12.     public function __construct(string $dataClass, array $validationGroups = [])
  13.     {
  14.         parent::__construct($dataClass$validationGroups);
  15.     }
  16.     public function buildForm(FormBuilderInterface $builder, array $options): void
  17.     {
  18.         $builder
  19.             ->remove('type')
  20.             ->add('file'FileType::class, ['label' => 'sylius.form.image.file'])
  21.             ->addEventListener(FormEvents::PRE_SET_DATA, [$this'onPreSetData']);
  22.     }
  23.     public function onPreSetData(FormEvent $event): void
  24.     {
  25.         $data $event->getData();
  26.         if (! ($data instanceof Customer)) {
  27.             return;
  28.         }
  29.         $event->setData($data->getImage());
  30.     }
  31.     public function getBlockPrefix(): string
  32.     {
  33.         return 'app_customer_photo_type';
  34.     }
  35. }