Skip to content

Commit

Permalink
Merge pull request #532 from BitBagCommerce/feature/OP-543
Browse files Browse the repository at this point in the history
OP-543: Content elements translatable
  • Loading branch information
senghe authored Sep 16, 2024
2 parents 2943635 + c31c6c5 commit 3740621
Show file tree
Hide file tree
Showing 27 changed files with 647 additions and 379 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Sylius\CmsPlugin\Entity\ContentConfigurationInterface;
use Sylius\CmsPlugin\Form\Type\ContentElements\PagesCollectionContentElementType;
use Sylius\CmsPlugin\Renderer\ContentElement\AbstractContentElement;
use Sylius\CmsPlugin\Renderer\ContentElement\ContentElementRendererInterface;
use Sylius\CmsPlugin\Renderer\ContentElement\PagesCollectionContentElementRenderer;
use Sylius\CmsPlugin\Repository\CollectionRepositoryInterface;
use Twig\Environment;
Expand Down
91 changes: 45 additions & 46 deletions spec/Renderer/ContentElementRendererStrategySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,79 +10,78 @@
use Sylius\CmsPlugin\Entity\ContentConfigurationInterface;
use Sylius\CmsPlugin\Entity\PageInterface;
use Sylius\CmsPlugin\Renderer\ContentElement\ContentElementRendererInterface;
use Sylius\CmsPlugin\Renderer\ContentElementRendererStrategy;
use Sylius\CmsPlugin\Renderer\ContentElementRendererStrategyInterface;
use Sylius\CmsPlugin\Twig\Parser\ContentParserInterface;
use Sylius\Component\Locale\Context\LocaleContextInterface;

final class ContentElementRendererStrategySpec extends ObjectBehavior
{
public function let(
ContentParserInterface $contentParser,
ContentElementRendererInterface $renderer1,
ContentElementRendererInterface $renderer2,
LocaleContextInterface $localeContext,
ContentElementRendererInterface $renderer,
): void {
$this->beConstructedWith($contentParser, [$renderer1, $renderer2]);
}

public function it_is_initializable(): void
{
$this->shouldHaveType(ContentElementRendererStrategy::class);
$this->beConstructedWith($contentParser, $localeContext, [$renderer]);
}

public function it_implements_content_element_renderer_strategy_interface(): void
{
$this->shouldImplement(ContentElementRendererStrategyInterface::class);
}

public function it_renders_content_elements_using_registered_renderers(
public function it_renders_a_page_content_element_correctly(
PageInterface $page,
ContentConfigurationInterface $contentElement,
LocaleContextInterface $localeContext,
ContentElementRendererInterface $renderer,
ContentParserInterface $contentParser,
ContentElementRendererInterface $renderer1,
ContentElementRendererInterface $renderer2,
BlockInterface $block,
ContentConfigurationInterface $contentElement1,
ContentConfigurationInterface $contentElement2,
): void {
$block->getContentElements()->willReturn(
new ArrayCollection([$contentElement1->getWrappedObject(), $contentElement2->getWrappedObject()]),
);

$renderer1->supports($contentElement1)->willReturn(true);
$renderer1->supports($contentElement2)->willReturn(false);
$renderer1->render($contentElement1)->willReturn('Rendered content 1');
$renderer2->supports($contentElement2)->willReturn(true);
$renderer2->supports($contentElement1)->willReturn(false);
$renderer2->render($contentElement2)->willReturn('Rendered content 2');
$page->getContentElements()->willReturn(new ArrayCollection([$contentElement->getWrappedObject()]));
$localeContext->getLocaleCode()->willReturn('en_US');
$contentElement->getLocale()->willReturn('en_US');

$expectedParsedContent = 'Parsed content after rendering';
$renderer->supports($contentElement)->willReturn(true);
$renderer->render($contentElement)->willReturn('<p>Hello World</p>');

$contentParser->parse('Rendered content 1Rendered content 2')->willReturn($expectedParsedContent);
$contentParser->parse('<p>Hello World</p>')->willReturn('<p>Hello World</p>');

$this->render($block)->shouldReturn($expectedParsedContent);
$this->render($page)->shouldReturn('<p>Hello World</p>');
}

public function it_renders_content_elements_using_registered_renderers_for_page(
public function it_skips_content_element_with_non_matching_locale(
BlockInterface $block,
ContentConfigurationInterface $contentElement,
LocaleContextInterface $localeContext,
ContentParserInterface $contentParser,
ContentElementRendererInterface $renderer1,
ContentElementRendererInterface $renderer2,
PageInterface $page,
ContentConfigurationInterface $contentElement1,
ContentConfigurationInterface $contentElement2,
): void {
$page->getContentElements()->willReturn(
new ArrayCollection([$contentElement1->getWrappedObject(), $contentElement2->getWrappedObject()]),
);
$block->getContentElements()->willReturn(new ArrayCollection([$contentElement]));
$localeContext->getLocaleCode()->willReturn('en_US');
$contentElement->getLocale()->willReturn('fr_FR');

$contentParser->parse('')->willReturn('');

$this->render($block)->shouldReturn('');
}

$renderer1->supports($contentElement1)->willReturn(true);
$renderer1->supports($contentElement2)->willReturn(false);
$renderer1->render($contentElement1)->willReturn('Rendered content 1');
$renderer2->supports($contentElement2)->willReturn(true);
$renderer2->supports($contentElement1)->willReturn(false);
$renderer2->render($contentElement2)->willReturn('Rendered content 2');
public function it_renders_only_supported_content_elements(
BlockInterface $block,
ContentConfigurationInterface $supportedElement,
ContentConfigurationInterface $unsupportedElement,
LocaleContextInterface $localeContext,
ContentElementRendererInterface $renderer,
ContentParserInterface $contentParser,
): void {
$block->getContentElements()->willReturn(new ArrayCollection([$supportedElement->getWrappedObject(), $unsupportedElement->getWrappedObject()]));
$localeContext->getLocaleCode()->willReturn('en_US');
$supportedElement->getLocale()->willReturn('en_US');
$unsupportedElement->getLocale()->willReturn('en_US');

$expectedParsedContent = 'Parsed content after rendering';
$renderer->supports($supportedElement)->willReturn(true);
$renderer->render($supportedElement)->willReturn('&lt;p&gt;Supported&lt;/p&gt;');
$renderer->supports($unsupportedElement)->willReturn(false);

$contentParser->parse('Rendered content 1Rendered content 2')->willReturn($expectedParsedContent);
$contentParser->parse('<p>Supported</p>')->willReturn('<p>Supported</p>');

$this->render($page)->shouldReturn($expectedParsedContent);
$this->render($block)->shouldReturn('<p>Supported</p>');
}
}
10 changes: 5 additions & 5 deletions spec/Twig/Runtime/TranslationFormReduceRuntimeSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ public function it_reduces_form_to_specified_fields(
FormView $form,
FormView $localeForm,
FormView $slugForm,
FormView $titleForm
FormView $titleForm,
): void {
$form->children = [
'en_US' => $localeForm
'en_US' => $localeForm,
];

$localeForm->children = [
Expand Down Expand Up @@ -56,7 +56,7 @@ public function it_handles_multiple_locales(
FormView $enLocale,
FormView $deLocale,
FormView $slugForm,
FormView $titleForm
FormView $titleForm,
): void {
$form->children = [
'en_US' => $enLocale,
Expand Down Expand Up @@ -85,7 +85,7 @@ public function it_throws_exception_if_field_is_not_present_in_multiple_locales(
FormView $form,
FormView $enLocale,
FormView $deLocale,
FormView $slugForm
FormView $slugForm,
): void {
$form->children = [
'en_US' => $enLocale,
Expand All @@ -108,7 +108,7 @@ public function it_handles_empty_field_array(
FormView $form,
FormView $localeForm,
FormView $slugForm,
FormView $titleForm
FormView $titleForm,
): void {
$form->children = [
'en_US' => $localeForm,
Expand Down
12 changes: 12 additions & 0 deletions src/Entity/ContentConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class ContentConfiguration implements ContentConfigurationInterface

protected array $configuration = [];

protected ?string $locale = null;

protected ?BlockInterface $block = null;

protected ?PageInterface $page = null;
Expand Down Expand Up @@ -41,6 +43,16 @@ public function setConfiguration(array $configuration): void
$this->configuration = $configuration;
}

public function getLocale(): ?string
{
return $this->locale;
}

public function setLocale(?string $locale): void
{
$this->locale = $locale;
}

public function getBlock(): ?BlockInterface
{
return $this->block;
Expand Down
4 changes: 4 additions & 0 deletions src/Entity/ContentConfigurationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ public function getConfiguration(): array;

public function setConfiguration(array $configuration): void;

public function getLocale(): ?string;

public function setLocale(?string $locale): void;

public function getBlock(): ?BlockInterface;

public function setBlock(?BlockInterface $block): void;
Expand Down
5 changes: 3 additions & 2 deletions src/Fixture/BlockFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ protected function configureOptionsNode(ArrayNodeDefinition $optionsNode): void
->arrayNode('taxons')->scalarPrototype()->end()->end()
->arrayNode('products_in_taxons')->scalarPrototype()->end()->end()
->arrayNode('content_elements')
->useAttributeAsKey('key')
->useAttributeAsKey('locale')
->arrayPrototype()
->useAttributeAsKey('key')
->arrayPrototype()
->children()
->scalarNode('type')->end()
Expand All @@ -54,7 +56,6 @@ protected function configureOptionsNode(ArrayNodeDefinition $optionsNode): void
->scalarNode('products_carousel_by_taxon')->end()
->scalarNode('products_grid_by_taxon')->end()
->scalarNode('pages_collection')->end()
->scalarNode('pages_collection')->end()
->scalarNode('spacer')->end()
->arrayNode('multiple_media')->scalarPrototype()->end()->end()
->arrayNode('products_grid')
Expand Down
21 changes: 12 additions & 9 deletions src/Fixture/Factory/BlockFixtureFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,19 @@ private function createBlock(string $code, array $blockData): void
$this->taxonsAssigner->assign($block, $blockData['taxons']);
$this->productsInTaxonsAssigner->assign($block, $blockData['products_in_taxons']);

foreach ($blockData['content_elements'] as $data) {
$data['data'] = array_filter($data['data'], static function ($value) {
return !empty($value);
});
foreach ($blockData['content_elements'] as $locale => $data) {
foreach ($data as $contentElementData) {
$contentElementData['data'] = array_filter($contentElementData['data'], static function ($value) {
return !empty($value);
});

$contentConfiguration = new ContentConfiguration();
$contentConfiguration->setType($data['type']);
$contentConfiguration->setConfiguration($data['data']);
$contentConfiguration->setBlock($block);
$block->addContentElement($contentConfiguration);
$contentConfiguration = new ContentConfiguration();
$contentConfiguration->setType($contentElementData['type']);
$contentConfiguration->setConfiguration($contentElementData['data']);
$contentConfiguration->setLocale($locale);
$contentConfiguration->setBlock($block);
$block->addContentElement($contentConfiguration);
}
}

$this->blockRepository->add($block);
Expand Down
23 changes: 13 additions & 10 deletions src/Fixture/Factory/PageFixtureFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,19 @@ private function createPage(
$page->addTranslation($pageTranslation);
}

foreach ($pageData['content_elements'] as $data) {
$data['data'] = array_filter($data['data'], static function ($value) {
return !empty($value);
});

$contentConfiguration = new ContentConfiguration();
$contentConfiguration->setType($data['type']);
$contentConfiguration->setConfiguration($data['data']);
$contentConfiguration->setPage($page);
$page->addContentElement($contentConfiguration);
foreach ($pageData['content_elements'] as $locale => $data) {
foreach ($data as $contentElementData) {
$contentElementData['data'] = array_filter($contentElementData['data'], static function ($value) {
return !empty($value);
});

$contentConfiguration = new ContentConfiguration();
$contentConfiguration->setType($contentElementData['type']);
$contentConfiguration->setConfiguration($contentElementData['data']);
$contentConfiguration->setLocale($locale);
$contentConfiguration->setPage($page);
$page->addContentElement($contentConfiguration);
}
}

$this->pageRepository->add($page);
Expand Down
4 changes: 3 additions & 1 deletion src/Fixture/PageFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ protected function configureOptionsNode(ArrayNodeDefinition $optionsNode): void
->end()
->end()
->arrayNode('content_elements')
->useAttributeAsKey('key')
->useAttributeAsKey('locale')
->arrayPrototype()
->useAttributeAsKey('key')
->arrayPrototype()
->children()
->scalarNode('type')->end()
Expand Down
35 changes: 35 additions & 0 deletions src/Form/Type/BlockType.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,32 @@
use Sylius\Bundle\ResourceBundle\Form\Type\AbstractResourceType;
use Sylius\Bundle\TaxonomyBundle\Form\Type\TaxonAutocompleteChoiceType;
use Sylius\CmsPlugin\Entity\BlockInterface;
use Sylius\Component\Locale\Model\LocaleInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;

final class BlockType extends AbstractResourceType
{
private array $locales = [];

public function __construct(
private RepositoryInterface $localeRepository,
string $dataClass,
array $validationGroups = [],
) {
parent::__construct($dataClass, $validationGroups);

/** @var LocaleInterface[] $locales */
$locales = $this->localeRepository->findAll();
foreach ($locales as $locale) {
$this->locales[$locale->getName()] = $locale->getCode();
}
}

public function buildForm(FormBuilderInterface $builder, array $options): void
{
/** @var BlockInterface $block */
Expand Down Expand Up @@ -50,6 +69,12 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
'allow_delete' => true,
'by_reference' => false,
'required' => false,
'entry_options' => [
'label' => false,
],
'attr' => [
'class' => 'content-elements-container',
],
])
->add('products', ProductAutocompleteChoiceType::class, [
'label' => 'sylius_cms.ui.display_for_products.label',
Expand All @@ -70,7 +95,17 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
'label' => false,
'mapped' => false,
])
->add('locale', ChoiceType::class, [
'choices' => $this->locales,
'mapped' => false,
'label' => 'sylius.ui.locale',
'attr' => [
'class' => 'locale-selector',
],
])
;

PageType::addContentElementLocaleListener($builder);
}

public function getBlockPrefix(): string
Expand Down
2 changes: 2 additions & 0 deletions src/Form/Type/ContentConfigurationType.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Sylius\Bundle\ResourceBundle\Form\Type\AbstractResourceType;
use Sylius\CmsPlugin\Entity\ContentConfigurationInterface;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
Expand Down Expand Up @@ -38,6 +39,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
$defaultActionConfigurationType = $this->actionConfigurationTypes[$defaultActionType];

$builder
->add('locale', HiddenType::class)
->add('type', ChoiceType::class, [
'label' => 'sylius.ui.type',
'choices' => $this->actionTypes,
Expand Down
Loading

0 comments on commit 3740621

Please sign in to comment.