Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OP-494: Fix for error 500 when trying to edit block/page when associated element has been removed #526

Merged
merged 2 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,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;
Expand All @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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
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>
Loading