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..a2ffc366 100644
--- a/src/Form/Type/ContentElements/PagesCollectionContentElementType.php
+++ b/src/Form/Type/ContentElements/PagesCollectionContentElementType.php
@@ -10,6 +10,7 @@
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;
@@ -21,8 +22,10 @@ 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 +39,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..d39aa28f 100644
--- a/src/Form/Type/ContentElements/ProductsCarouselByTaxonContentElementType.php
+++ b/src/Form/Type/ContentElements/ProductsCarouselByTaxonContentElementType.php
@@ -10,6 +10,7 @@
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;
@@ -21,8 +22,10 @@ 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 +41,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..66cc543f 100644
--- a/src/Form/Type/ContentElements/ProductsGridByTaxonContentElementType.php
+++ b/src/Form/Type/ContentElements/ProductsGridByTaxonContentElementType.php
@@ -10,6 +10,7 @@
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;
@@ -21,8 +22,10 @@ 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 +41,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..8fdd7075 100644
--- a/src/Form/Type/ContentElements/SingleMediaContentElementType.php
+++ b/src/Form/Type/ContentElements/SingleMediaContentElementType.php
@@ -10,6 +10,7 @@
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;
@@ -21,8 +22,10 @@ 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 +39,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 @@
-
+
+
+