From e356d8d7eff8f75210347658ce7bf25d21186a1f Mon Sep 17 00:00:00 2001 From: jkindly Date: Thu, 22 Aug 2024 12:32:17 +0200 Subject: [PATCH] OP-494: Fix for error 500 when trying to edit block/page when media associated in content element has been removed --- .../ContentElementDataTransformerChecker.php | 32 +++++++++++++++++++ .../PagesCollectionContentElementType.php | 11 +++++-- ...ductsCarouselByTaxonContentElementType.php | 11 +++++-- .../ProductsGridByTaxonContentElementType.php | 11 +++++-- .../SingleMediaContentElementType.php | 11 +++++-- src/Resources/config/services/form.xml | 10 ++++-- 6 files changed, 76 insertions(+), 10 deletions(-) create mode 100644 src/Form/DataTransformer/ContentElementDataTransformerChecker.php diff --git a/src/Form/DataTransformer/ContentElementDataTransformerChecker.php b/src/Form/DataTransformer/ContentElementDataTransformerChecker.php new file mode 100644 index 00000000..93afe41a --- /dev/null +++ b/src/Form/DataTransformer/ContentElementDataTransformerChecker.php @@ -0,0 +1,32 @@ +addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($repository, $field): void { + $data = $event->getData(); + $code = $data[$field] ?? null; + $entity = $repository->findOneBy(['code' => $code]); + if (null === $entity) { + $data[$field] = null; + $event->setData($data); + } + }); + } +} diff --git a/src/Form/Type/ContentElements/PagesCollectionContentElementType.php b/src/Form/Type/ContentElements/PagesCollectionContentElementType.php index d076945e..23eaf96b 100644 --- a/src/Form/Type/ContentElements/PagesCollectionContentElementType.php +++ b/src/Form/Type/ContentElements/PagesCollectionContentElementType.php @@ -10,19 +10,24 @@ namespace BitBag\SyliusCmsPlugin\Form\Type\ContentElements; +use BitBag\SyliusCmsPlugin\Form\DataTransformer\ContentElementDataTransformerChecker; use BitBag\SyliusCmsPlugin\Form\Type\PageCollectionAutocompleteChoiceType; use Sylius\Bundle\ResourceBundle\Form\DataTransformer\ResourceToIdentifierTransformer; use Sylius\Component\Resource\Repository\RepositoryInterface; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; +use Symfony\Component\Form\FormEvent; +use Symfony\Component\Form\FormEvents; use Symfony\Component\Form\ReversedTransformer; final class PagesCollectionContentElementType extends AbstractType { public const TYPE = 'pages_collection'; - public function __construct(private RepositoryInterface $collectionRepository) - { + public function __construct( + private RepositoryInterface $collectionRepository, + private ContentElementDataTransformerChecker $contentElementDataTransformerChecker, + ) { } public function buildForm(FormBuilderInterface $builder, array $options): void @@ -36,6 +41,8 @@ public function buildForm(FormBuilderInterface $builder, array $options): void $builder->get(self::TYPE)->addModelTransformer( new ReversedTransformer(new ResourceToIdentifierTransformer($this->collectionRepository, 'code')), ); + + $this->contentElementDataTransformerChecker->check($builder, $this->collectionRepository, self::TYPE); } public function getBlockPrefix(): string diff --git a/src/Form/Type/ContentElements/ProductsCarouselByTaxonContentElementType.php b/src/Form/Type/ContentElements/ProductsCarouselByTaxonContentElementType.php index aed6a44a..7e544b06 100644 --- a/src/Form/Type/ContentElements/ProductsCarouselByTaxonContentElementType.php +++ b/src/Form/Type/ContentElements/ProductsCarouselByTaxonContentElementType.php @@ -10,19 +10,24 @@ namespace BitBag\SyliusCmsPlugin\Form\Type\ContentElements; +use BitBag\SyliusCmsPlugin\Form\DataTransformer\ContentElementDataTransformerChecker; use Sylius\Bundle\ResourceBundle\Form\DataTransformer\ResourceToIdentifierTransformer; use Sylius\Bundle\TaxonomyBundle\Form\Type\TaxonAutocompleteChoiceType; use Sylius\Component\Resource\Repository\RepositoryInterface; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; +use Symfony\Component\Form\FormEvent; +use Symfony\Component\Form\FormEvents; use Symfony\Component\Form\ReversedTransformer; final class ProductsCarouselByTaxonContentElementType extends AbstractType { public const TYPE = 'products_carousel_by_taxon'; - public function __construct(private RepositoryInterface $taxonRepository) - { + public function __construct( + private RepositoryInterface $taxonRepository, + private ContentElementDataTransformerChecker $contentElementDataTransformerChecker, + ) { } public function buildForm(FormBuilderInterface $builder, array $options): void @@ -38,6 +43,8 @@ public function buildForm(FormBuilderInterface $builder, array $options): void $builder->get(self::TYPE)->addModelTransformer( new ReversedTransformer(new ResourceToIdentifierTransformer($this->taxonRepository, 'code')), ); + + $this->contentElementDataTransformerChecker->check($builder, $this->taxonRepository, self::TYPE); } public function getBlockPrefix(): string diff --git a/src/Form/Type/ContentElements/ProductsGridByTaxonContentElementType.php b/src/Form/Type/ContentElements/ProductsGridByTaxonContentElementType.php index 9721f89d..9dc5f6f5 100644 --- a/src/Form/Type/ContentElements/ProductsGridByTaxonContentElementType.php +++ b/src/Form/Type/ContentElements/ProductsGridByTaxonContentElementType.php @@ -10,19 +10,24 @@ namespace BitBag\SyliusCmsPlugin\Form\Type\ContentElements; +use BitBag\SyliusCmsPlugin\Form\DataTransformer\ContentElementDataTransformerChecker; use Sylius\Bundle\ResourceBundle\Form\DataTransformer\ResourceToIdentifierTransformer; use Sylius\Bundle\TaxonomyBundle\Form\Type\TaxonAutocompleteChoiceType; use Sylius\Component\Resource\Repository\RepositoryInterface; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; +use Symfony\Component\Form\FormEvent; +use Symfony\Component\Form\FormEvents; use Symfony\Component\Form\ReversedTransformer; final class ProductsGridByTaxonContentElementType extends AbstractType { public const TYPE = 'products_grid_by_taxon'; - public function __construct(private RepositoryInterface $taxonRepository) - { + public function __construct( + private RepositoryInterface $taxonRepository, + private ContentElementDataTransformerChecker $contentElementDataTransformerChecker, + ) { } public function buildForm(FormBuilderInterface $builder, array $options): void @@ -38,6 +43,8 @@ public function buildForm(FormBuilderInterface $builder, array $options): void $builder->get(self::TYPE)->addModelTransformer( new ReversedTransformer(new ResourceToIdentifierTransformer($this->taxonRepository, 'code')), ); + + $this->contentElementDataTransformerChecker->check($builder, $this->taxonRepository, self::TYPE); } public function getBlockPrefix(): string diff --git a/src/Form/Type/ContentElements/SingleMediaContentElementType.php b/src/Form/Type/ContentElements/SingleMediaContentElementType.php index e761c4a7..2d562eb5 100644 --- a/src/Form/Type/ContentElements/SingleMediaContentElementType.php +++ b/src/Form/Type/ContentElements/SingleMediaContentElementType.php @@ -10,19 +10,24 @@ namespace BitBag\SyliusCmsPlugin\Form\Type\ContentElements; +use BitBag\SyliusCmsPlugin\Form\DataTransformer\ContentElementDataTransformerChecker; use BitBag\SyliusCmsPlugin\Form\Type\MediaAutocompleteChoiceType; use Sylius\Bundle\ResourceBundle\Form\DataTransformer\ResourceToIdentifierTransformer; use Sylius\Component\Resource\Repository\RepositoryInterface; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; +use Symfony\Component\Form\FormEvent; +use Symfony\Component\Form\FormEvents; use Symfony\Component\Form\ReversedTransformer; final class SingleMediaContentElementType extends AbstractType { public const TYPE = 'single_media'; - public function __construct(private RepositoryInterface $mediaRepository) - { + public function __construct( + private RepositoryInterface $mediaRepository, + private ContentElementDataTransformerChecker $contentElementDataTransformerChecker, + ) { } public function buildForm(FormBuilderInterface $builder, array $options): void @@ -36,6 +41,8 @@ public function buildForm(FormBuilderInterface $builder, array $options): void $builder->get(self::TYPE)->addModelTransformer( new ReversedTransformer(new ResourceToIdentifierTransformer($this->mediaRepository, 'code')), ); + + $this->contentElementDataTransformerChecker->check($builder, $this->mediaRepository, self::TYPE); } public function getBlockPrefix(): string diff --git a/src/Resources/config/services/form.xml b/src/Resources/config/services/form.xml index 5c917166..799016e5 100644 --- a/src/Resources/config/services/form.xml +++ b/src/Resources/config/services/form.xml @@ -96,12 +96,13 @@ + - + @@ -118,6 +119,7 @@ + @@ -129,6 +131,7 @@ + @@ -140,6 +143,7 @@ + @@ -149,9 +153,11 @@ - + + +