Skip to content

Commit

Permalink
OP-494: Fix for error 500 when trying to edit block/page when media a…
Browse files Browse the repository at this point in the history
…ssociated in content element has been removed
  • Loading branch information
jkindly committed Aug 22, 2024
1 parent 19ef10c commit e356d8d
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 10 deletions.
32 changes: 32 additions & 0 deletions src/Form/DataTransformer/ContentElementDataTransformerChecker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/*
* This file was created by developers working at BitBag
* Do you need more information about us and what we do? Visit our https://bitbag.io website!
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
*/

declare(strict_types=1);

namespace BitBag\SyliusCmsPlugin\Form\DataTransformer;

use Sylius\Component\Resource\Repository\RepositoryInterface;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;

final class ContentElementDataTransformerChecker
{
public function check(FormBuilderInterface $builder, RepositoryInterface $repository, string $field): void
{
$builder->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);
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
11 changes: 9 additions & 2 deletions src/Form/Type/ContentElements/SingleMediaContentElementType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
10 changes: 8 additions & 2 deletions src/Resources/config/services/form.xml
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,13 @@

<service id="bitbag_sylius_cms_plugin.form.type.content_element.single_media" class="BitBag\SyliusCmsPlugin\Form\Type\ContentElements\SingleMediaContentElementType">
<argument type="service" id="bitbag_sylius_cms_plugin.repository.media" />
<argument type="service" id="bitbag_sylius_cms_plugin.form.data_transformer.content_element_checker" />
<tag name="bitbag_sylius_cms_plugin.content_elements.type" key="%bitbag_sylius_cms_plugin.content_elements.type.single_media%" />
<tag name="form.type" />
</service>

<service id="bitbag_sylius_cms_plugin.form.type.content_element.multiple_media" class="BitBag\SyliusCmsPlugin\Form\Type\ContentElements\MultipleMediaContentElementType">
<argument type="service" id="bitbag_sylius_cms_plugin.form.type.data_transformer.multiple_media_to_codes" />
<argument type="service" id="bitbag_sylius_cms_plugin.form.data_transformer.multiple_media_to_codes" />
<tag name="bitbag_sylius_cms_plugin.content_elements.type" key="%bitbag_sylius_cms_plugin.content_elements.type.multiple_media%" />
<tag name="form.type" />
</service>
Expand All @@ -118,6 +119,7 @@

<service id="bitbag_sylius_cms_plugin.form.type.content_element.products_carousel_by_taxon" class="BitBag\SyliusCmsPlugin\Form\Type\ContentElements\ProductsCarouselByTaxonContentElementType">
<argument type="service" id="sylius.repository.taxon" />
<argument type="service" id="bitbag_sylius_cms_plugin.form.data_transformer.content_element_checker" />
<tag name="bitbag_sylius_cms_plugin.content_elements.type" key="%bitbag_sylius_cms_plugin.content_elements.type.products_carousel_by_taxon%" />
<tag name="form.type" />
</service>
Expand All @@ -129,6 +131,7 @@

<service id="bitbag_sylius_cms_plugin.form.type.content_element.products_grid_by_taxon" class="BitBag\SyliusCmsPlugin\Form\Type\ContentElements\ProductsGridByTaxonContentElementType">
<argument type="service" id="sylius.repository.taxon" />
<argument type="service" id="bitbag_sylius_cms_plugin.form.data_transformer.content_element_checker" />
<tag name="bitbag_sylius_cms_plugin.content_elements.type" key="%bitbag_sylius_cms_plugin.content_elements.type.products_grid_by_taxon%" />
<tag name="form.type" />
</service>
Expand All @@ -140,6 +143,7 @@

<service id="bitbag_sylius_cms_plugin.form.type.content_element.pages_collection" class="BitBag\SyliusCmsPlugin\Form\Type\ContentElements\PagesCollectionContentElementType">
<argument type="service" id="bitbag_sylius_cms_plugin.repository.collection" />
<argument type="service" id="bitbag_sylius_cms_plugin.form.data_transformer.content_element_checker" />
<tag name="bitbag_sylius_cms_plugin.content_elements.type" key="%bitbag_sylius_cms_plugin.content_elements.type.pages_collection%" />
<tag name="form.type" />
</service>
Expand All @@ -149,9 +153,11 @@
<tag name="form.type" />
</service>

<service id="bitbag_sylius_cms_plugin.form.type.data_transformer.multiple_media_to_codes" class="BitBag\SyliusCmsPlugin\Form\DataTransformer\MultipleMediaToCodesTransformer">
<service id="bitbag_sylius_cms_plugin.form.data_transformer.multiple_media_to_codes" class="BitBag\SyliusCmsPlugin\Form\DataTransformer\MultipleMediaToCodesTransformer">
<argument type="service" id="bitbag_sylius_cms_plugin.repository.media" />
</service>

<service id="bitbag_sylius_cms_plugin.form.data_transformer.content_element_checker" class="BitBag\SyliusCmsPlugin\Form\DataTransformer\ContentElementDataTransformerChecker"/>
</services>

</container>

0 comments on commit e356d8d

Please sign in to comment.