From 4ba971015701aedc9a4be3d934c2eca5c5f4adc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Igor=20Ste=CC=A8pien=CC=81?= Date: Fri, 1 Mar 2024 01:54:11 +0100 Subject: [PATCH] form default data --- config/admin/form.yml | 6 ++ config/admin/services.yml | 1 + src/Entity/ImageSlider.php | 2 - .../ImagesliderFormSubscriber.php | 90 +++++++++++-------- src/Form/Type/ImageSliderType.php | 13 ++- .../Shop/ShopAssociationChoiceTreeType.php | 12 +-- 6 files changed, 75 insertions(+), 49 deletions(-) diff --git a/config/admin/form.yml b/config/admin/form.yml index dd33de9..8cb5b1d 100644 --- a/config/admin/form.yml +++ b/config/admin/form.yml @@ -5,6 +5,12 @@ services: arguments: - '@prestashop.core.admin.shop.repository' + Oksydan\IsImageslider\Form\EventListener\ImagesliderFormSubscriber: + class: 'Oksydan\IsImageslider\Form\EventListener\ImagesliderFormSubscriber' + autowire: false + arguments: + - '@prestashop.core.admin.shop.repository' + Oksydan\IsImageslider\Form\DataTransformer\Lang\LangModeDataTransformer: class: 'Oksydan\IsImageslider\Form\DataTransformer\Lang\LangModeDataTransformer' autowire: false diff --git a/config/admin/services.yml b/config/admin/services.yml index e745fcd..3a67c81 100644 --- a/config/admin/services.yml +++ b/config/admin/services.yml @@ -31,6 +31,7 @@ services: exclude: - '../../src/{Controller,Falconize,Domain}' - '../../src/Form/DataTransformer' + - '../../src/Form/EventListener' - '../../src/Form/Type/Shop' - '../../src/index.php' - '../../src/*/index.php' diff --git a/src/Entity/ImageSlider.php b/src/Entity/ImageSlider.php index 9d3fb1c..e79558b 100644 --- a/src/Entity/ImageSlider.php +++ b/src/Entity/ImageSlider.php @@ -81,8 +81,6 @@ public function __construct() { $this->shops = new ArrayCollection(); $this->sliderLang = new ArrayCollection(); - $this->active = false; - $this->image_to_all_langs = false; } public function getId(): int diff --git a/src/Form/EventListener/ImagesliderFormSubscriber.php b/src/Form/EventListener/ImagesliderFormSubscriber.php index 5083fde..b0c1023 100644 --- a/src/Form/EventListener/ImagesliderFormSubscriber.php +++ b/src/Form/EventListener/ImagesliderFormSubscriber.php @@ -4,65 +4,85 @@ namespace Oksydan\IsImageslider\Form\EventListener; +use PrestaShopBundle\Entity\Repository\ShopRepository; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\Form\FormEvent; use Symfony\Component\Form\FormEvents; -use Symfony\Component\Validator\Constraints\File; -use Symfony\Component\Validator\Constraints\NotBlank; +use Symfony\Component\Form\FormInterface; class ImagesliderFormSubscriber implements EventSubscriberInterface { + private ShopRepository $shopRepository; + + public function __construct(ShopRepository $shopRepository) + { + $this->shopRepository = $shopRepository; + } + public static function getSubscribedEvents(): array { return [ - FormEvents::PRE_SUBMIT => 'preSubmit', - FormEvents::PRE_SET_DATA => 'preSetData', +// FormEvents::PRE_SUBMIT => 'preSubmit', + FormEvents::POST_SET_DATA => 'postSetData', ]; } - private function modifyFormBasedOnSelectedType($form, $data) + private function removeImageField(string $name, FormInterface $form) { - $isEdit = $data['edit'] ?? false; - $imagesForAllLangs = $data['images_for_all_langs'] ?? false; - - $imageConstrains = [ - new File([ - 'mimeTypes' => [ - 'image/gif', - 'image/jpeg', - 'image/png', - ], - ]), - ]; - - if (!$isEdit) { - $imageConstrains[] = new NotBlank(); + if ($form->has($name)) { + $form->remove($name); } - - // if ($imagesForAllLangs) { - // $form->add('images', ImagesType::class, [ - // 'imagesConstraints' => $imageConstrains, - // ]); - // } else { - // $form->add('images', ImagesMultilangType::class, [ - // 'imagesConstraints' => $imageConstrains, - // ]); - // } } - public function preSetData(FormEvent $event) + private function modifyFormBasedOnSelectedType(FormEvent $event) { - $data = $event->getData(); $form = $event->getForm(); + $imagesForAllLangs = $form->get('images_for_all_langs')->getData() !== null ? $form->get('images_for_all_langs')->getData() : false; - $this->modifyFormBasedOnSelectedType($form, $data); + if ($imagesForAllLangs) { + if ($form->has('slider_langs')) { + $langsForm = $form->get('slider_langs'); + + foreach ($langsForm->all() as $langForm) { + $this->removeImageField('image', $langForm); + $this->removeImageField('image_mobile', $langForm); + } + } + } else { + $this->removeImageField('image', $form); + $this->removeImageField('image_mobile', $form); + } } public function preSubmit(FormEvent $event): void { - $data = $event->getData(); + $this->modifyFormBasedOnSelectedType($event); + } + + private function setDefaultData(FormEvent $event) + { + $imageSlider = $event->getData(); $form = $event->getForm(); - $this->modifyFormBasedOnSelectedType($form, $data); + // Set default values if the entity is empty (new form) + if (null === $imageSlider || null === $imageSlider->getId()) { + $form->get('display_from')->setData(new \DateTime()); + $form->get('display_to')->setData((new \DateTime())->modify('+1 month')); + + if ($form->has('shop_association')) { + $shops = $this->shopRepository->findBy(['active' => true]); + $shops = array_map(function ($shop) { + return $shop->getId(); + }, $shops); + + $form->get('shop_association')->setData($shops); + } + } + } + + public function postSetData(FormEvent $event) + { + $this->setDefaultData($event); + $this->modifyFormBasedOnSelectedType($event); } } diff --git a/src/Form/Type/ImageSliderType.php b/src/Form/Type/ImageSliderType.php index 7ce6a18..a56cc79 100644 --- a/src/Form/Type/ImageSliderType.php +++ b/src/Form/Type/ImageSliderType.php @@ -10,12 +10,15 @@ use Oksydan\IsImageslider\Form\Type\Lang\LangFieldsType; use Oksydan\IsImageslider\Form\Type\Shop\ShopAssociationChoiceTreeType; use Oksydan\IsImageslider\Form\Type\Slider\ImageSliderLangType; +use Oksydan\IsImageslider\Form\Type\Slider\ImageType; use Oksydan\IsImageslider\Translations\TranslationDomains; use PrestaShop\PrestaShop\Adapter\Feature\MultistoreFeature; use PrestaShopBundle\Form\Admin\Type\SwitchType; use PrestaShopBundle\Form\Admin\Type\TranslatorAwareType; use Symfony\Component\Form\Extension\Core\Type\DateTimeType; use Symfony\Component\Form\FormBuilderInterface; +use Symfony\Component\Form\FormEvent; +use Symfony\Component\Form\FormEvents; use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\Validator\Constraints\NotBlank; use Symfony\Contracts\Translation\TranslatorInterface; @@ -56,6 +59,14 @@ public function buildForm(FormBuilderInterface $builder, array $options): void 'class' => 'js-toggle-images-types', ], ]) + ->add('image', ImageType::class, [ + 'label' => $this->trans('Image', TranslationDomains::TRANSLATION_DOMAIN_ADMIN), + 'required' => false, + ]) + ->add('image_mobile', ImageType::class, [ + 'label' => $this->trans('Image mobile', TranslationDomains::TRANSLATION_DOMAIN_ADMIN), + 'required' => false, + ]) ->add('slider_langs', LangFieldsType::class, [ 'entry_type' => ImageSliderLangType::class, 'entity_class' => ImageSliderLang::class, @@ -96,7 +107,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void ]); } - // $builder->addEventSubscriber($this->imagesliderFormSubscriber); + $builder->addEventSubscriber($this->imagesliderFormSubscriber); } public function configureOptions(OptionsResolver $resolver): void diff --git a/src/Form/Type/Shop/ShopAssociationChoiceTreeType.php b/src/Form/Type/Shop/ShopAssociationChoiceTreeType.php index e4fe26e..05a0bdc 100644 --- a/src/Form/Type/Shop/ShopAssociationChoiceTreeType.php +++ b/src/Form/Type/Shop/ShopAssociationChoiceTreeType.php @@ -9,6 +9,7 @@ use PrestaShopBundle\Form\Admin\Type\ShopChoiceTreeType; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; +use Symfony\Component\Form\FormEvent; use Symfony\Component\Form\FormEvents; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -35,21 +36,10 @@ public function __construct( public function buildForm(FormBuilderInterface $builder, array $options) { $builder->addModelTransformer($this->shopChoiceModelDataTransformer); - $builder->addEventListener( - FormEvents::PRE_SET_DATA, - [$this, 'onPreSetData'] - ); parent::buildForm($builder, $options); } - public function onPreSetData($event) - { - $data = $this->shopChoiceModelDataTransformer->transform($event->getData()); - - $event->setData($data); - } - /** * {@inheritdoc} */