<?php
namespace App\Form\Type\SiteHome;
use Sylius\Bundle\ResourceBundle\Form\Type\AbstractResourceType;
use Sylius\Bundle\ResourceBundle\Form\Type\ResourceTranslationsType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use App\Entity\SiteHome\SlideImage;
use App\Entity\Product\ProductVariant;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
class SlideType extends AbstractResourceType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('translations', ResourceTranslationsType::class, [
'entry_type' => SlideTranslationType::class,
])
->add('fabricProduct', EntityType::class, [
'label' => 'Choisir un produit tissu',
'class' => ProductVariant::class,
'query_builder' => function ($er) {
return $er->createQueryBuilder('o')
->innerJoin('o.translations', 'translation', 'WITH', 'translation.locale = :locale')
->andWhere('o.code LIKE :code')
->setParameter('locale', 'fr_FR')
->setParameter('code', 'TISS.%')
->orderBy('translation.name', 'ASC');
},
'choice_label' => 'name',
'choice_value' => 'id',
'required' => false,
'multiple' => false
])
->add('position', IntegerType::class, [
'label' => "sylius.ui.position",
'required' => false,
])
->add('enabled', CheckboxType::class, [
'label' => "sylius.ui.enabled",
'required' => false,
])
->add('dateDiffusion', DateTimeType::class, [
'label' => 'Date de diffusion',
'widget' => 'single_text',
'required' => true
])
->add('images', CollectionType::class, [
'entry_type' => SlideImageType::class,
'allow_add' => false,
'allow_delete' => false,
'by_reference' => false,
'required' => true,
'label' => 'Images'
])
->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($options) {
$data = $event->getData();
foreach (SlideImage::IMAGE_TYPES as $imageType) {
if (count($data->getImagesByType($imageType)) <= 0) {
$siteShopImage = new SlideImage();
$siteShopImage->setType($imageType);
$data->addImage($siteShopImage);
}
}
})
;
}
/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'app_site_home_slide';
}
}