Skip to content

Commit

Permalink
form default data
Browse files Browse the repository at this point in the history
  • Loading branch information
Oksydan committed Mar 1, 2024
1 parent 6ff84db commit 4ba9710
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 49 deletions.
6 changes: 6 additions & 0 deletions config/admin/form.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions config/admin/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 0 additions & 2 deletions src/Entity/ImageSlider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
90 changes: 55 additions & 35 deletions src/Form/EventListener/ImagesliderFormSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
13 changes: 12 additions & 1 deletion src/Form/Type/ImageSliderType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
12 changes: 1 addition & 11 deletions src/Form/Type/Shop/ShopAssociationChoiceTreeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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}
*/
Expand Down

0 comments on commit 4ba9710

Please sign in to comment.