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-515: Change service definition for content elements #530

Merged
merged 4 commits into from
Sep 10, 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
28 changes: 18 additions & 10 deletions doc/content_elements.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,34 +55,36 @@ final class TextContentElementType extends AbstractType
}
```

2. Define constant parameter in `config/parameters.yaml` or yours any other `yaml` file:
2. If your form type have constructor with some arguments, define constant parameter in `config/parameters.yaml` or yours any other `yaml` file:

```yaml
parameters:
sylius_cms.content_elements.type.text: !php/const 'YourNamespace\Form\Type\ContentElements\TextContentElementType::TYPE'
```

3. Define form type in service container under `config/services.yml` with correct tags:
If your form type doesn't have any constructor arguments, you can skip this step, because compiler pass will automatically define it for you.


3. If your form type have constructor with some arguments, you must define form type in service container under `config/services.yml` with correct tags:

```yaml
services:
sylius_cms.form.type.content_element.text:
class: YourNamespace\Form\Type\ContentElements\TextContentElementType
arguments: [...]
tags:
- { name: 'sylius_cms.content_elements.type', key: '%sylius_cms.content_elements.type.text%' }
- { name: 'form.type' }
```

4. Create a new renderer class under `src/Renderer/ContentElement` location. Implement `Sylius\CmsPlugin\Renderer\ContentElement\ContentElementRendererInterface` interface.
If your form type doesn't have any constructor arguments, you can skip this step, because compiler pass will automatically register it for you.

4. Create a new renderer class under `src/Renderer/ContentElement` location. Extend `Sylius\CmsPlugin\Renderer\ContentElement\AbstractContentElement` class.
For example, you can create a new renderer called `TextContentElementRenderer`:

```php
final class TextContentElementRenderer implements ContentElementRendererInterface
final class TextContentElementRenderer extends AbstractContentElement
{
public function __construct(private Environment $twig)
{
}

public function supports(ContentConfigurationInterface $contentConfiguration): bool
{
return TextContentElementType::TYPE === $contentConfiguration->getType();
Expand All @@ -93,7 +95,7 @@ final class TextContentElementRenderer implements ContentElementRendererInterfac
$text = $contentConfiguration->getConfiguration()['text'];

return $this->twig->render('@SyliusCmsPlugin/Shop/ContentElement/index.html.twig', [
'content_element' => '@YourNamespace/Shop/ContentElement/_text.html.twig',
'content_element' => $this->template,
'text' => $text,
]);
}
Expand All @@ -109,9 +111,15 @@ services:
arguments:
- '@twig'
tags:
- { name: 'sylius_cms.renderer.content_element' }
- {
name: 'sylius_cms.renderer.content_element',
template: '@YourNamespace/Shop/ContentElement/_text.html.twig',
form_type: 'YourNamespace\Form\Type\ContentElements\TextContentElementType'
}
```

Define form_type only if your form type doesn't have constructor with additional arguments.

6. Finally, create a new template under `templates/bundles/SyliusCmsPlugin/Shop/ContentElement` location.
For example, you can create a new template called `_text.html.twig`:

Expand Down
1 change: 0 additions & 1 deletion features/admin/adding_block.feature
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@ Feature: Adding blocks
And I fill the code with "intro"
And I fill the name with "Intro"
And I select "Homepage" template
And I click button to use this template
And I confirm that I want to use this template
And I add it
Then I should be notified that the block has been created
Expand Down
1 change: 0 additions & 1 deletion features/admin/adding_page.feature
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,6 @@ Feature: Adding new page
And I fill the slug with "my_page"
And I fill the name with "My page"
And I select "Homepage" template
And I click button to use this template
And I confirm that I want to use this template
And I add it
Then I should be notified that the page has been created
Expand Down
19 changes: 7 additions & 12 deletions spec/Renderer/ContentElement/HeadingContentElementRendererSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,16 @@
use PhpSpec\ObjectBehavior;
use Sylius\CmsPlugin\Entity\ContentConfigurationInterface;
use Sylius\CmsPlugin\Form\Type\ContentElements\HeadingContentElementType;
use Sylius\CmsPlugin\Renderer\ContentElement\ContentElementRendererInterface;
use Sylius\CmsPlugin\Renderer\ContentElement\AbstractContentElement;
use Sylius\CmsPlugin\Renderer\ContentElement\HeadingContentElementRenderer;
use Twig\Environment;

final class HeadingContentElementRendererSpec extends ObjectBehavior
{
public function let(Environment $twig): void
{
$this->beConstructedWith($twig);
}

public function it_is_initializable(): void
{
$this->shouldHaveType(HeadingContentElementRenderer::class);
}

public function it_implements_content_element_renderer_interface(): void
{
$this->shouldImplement(ContentElementRendererInterface::class);
$this->shouldBeAnInstanceOf(AbstractContentElement::class);
}

public function it_supports_heading_content_element_type(ContentConfigurationInterface $contentConfiguration): void
Expand All @@ -42,13 +33,17 @@ public function it_does_not_support_other_content_element_types(ContentConfigura

public function it_renders_heading_content_element(Environment $twig, ContentConfigurationInterface $contentConfiguration): void
{
$template = 'custom_template';
$this->setTemplate($template);
$this->setTwigEnvironment($twig);

$contentConfiguration->getConfiguration()->willReturn([
'heading_type' => 'h1',
'heading' => 'Sample Heading',
]);

$twig->render('@SyliusCmsPlugin/Shop/ContentElement/index.html.twig', [
'content_element' => '@SyliusCmsPlugin/Shop/ContentElement/_heading.html.twig',
'content_element' => $template,
'heading_type' => 'h1',
'heading_content' => 'Sample Heading',
])->willReturn('rendered template');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Sylius\CmsPlugin\Entity\ContentConfigurationInterface;
use Sylius\CmsPlugin\Entity\MediaInterface;
use Sylius\CmsPlugin\Form\Type\ContentElements\MultipleMediaContentElementType;
use Sylius\CmsPlugin\Renderer\ContentElement\ContentElementRendererInterface;
use Sylius\CmsPlugin\Renderer\ContentElement\AbstractContentElement;
use Sylius\CmsPlugin\Renderer\ContentElement\MultipleMediaContentElementRenderer;
use Sylius\CmsPlugin\Repository\MediaRepositoryInterface;
use Sylius\CmsPlugin\Twig\Runtime\RenderMediaRuntimeInterface;
Expand All @@ -17,21 +17,16 @@
final class MultipleMediaContentElementRendererSpec extends ObjectBehavior
{
public function let(
Environment $twig,
RenderMediaRuntimeInterface $renderMediaRuntime,
MediaRepositoryInterface $mediaRepository,
): void {
$this->beConstructedWith($twig, $renderMediaRuntime, $mediaRepository);
$this->beConstructedWith($renderMediaRuntime, $mediaRepository);
}

public function it_is_initializable(): void
{
$this->shouldHaveType(MultipleMediaContentElementRenderer::class);
}

public function it_implements_content_element_renderer_interface(): void
{
$this->shouldImplement(ContentElementRendererInterface::class);
$this->shouldBeAnInstanceOf(AbstractContentElement::class);
}

public function it_supports_multiple_media_content_element_type(ContentConfigurationInterface $contentConfiguration): void
Expand All @@ -54,6 +49,10 @@ public function it_renders_multiple_media_content_element(
MediaInterface $media1,
MediaInterface $media2,
): void {
$template = 'custom_template';
$this->setTemplate($template);
$this->setTwigEnvironment($twig);

$contentConfiguration->getConfiguration()->willReturn([
'multiple_media' => ['code1', 'code2'],
]);
Expand All @@ -67,7 +66,7 @@ public function it_renders_multiple_media_content_element(
$renderMediaRuntime->renderMedia('code2')->willReturn('rendered media 2');

$twig->render('@SyliusCmsPlugin/Shop/ContentElement/index.html.twig', [
'content_element' => '@SyliusCmsPlugin/Shop/ContentElement/_multiple_media.html.twig',
'content_element' => $template,
'media' => [
[
'renderedContent' => 'rendered media 1',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,23 @@
use Sylius\CmsPlugin\Entity\CollectionInterface;
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;

final class PagesCollectionContentElementRendererSpec extends ObjectBehavior
{
public function let(Environment $twig, CollectionRepositoryInterface $collectionRepository): void
public function let(CollectionRepositoryInterface $collectionRepository): void
{
$this->beConstructedWith($twig, $collectionRepository);
$this->beConstructedWith($collectionRepository);
}

public function it_is_initializable(): void
{
$this->shouldHaveType(PagesCollectionContentElementRenderer::class);
}

public function it_implements_content_element_renderer_interface(): void
{
$this->shouldImplement(ContentElementRendererInterface::class);
$this->shouldBeAnInstanceOf(AbstractContentElement::class);
}

public function it_supports_pages_collection_content_element_type(ContentConfigurationInterface $contentConfiguration): void
Expand All @@ -49,6 +46,10 @@ public function it_renders_pages_collection_content_element(
ContentConfigurationInterface $contentConfiguration,
CollectionInterface $collection,
): void {
$template = 'custom_template';
$this->setTemplate($template);
$this->setTwigEnvironment($twig);

$contentConfiguration->getConfiguration()->willReturn([
'pages_collection' => 'collection_code',
]);
Expand All @@ -59,7 +60,7 @@ public function it_renders_pages_collection_content_element(
$collection->getPages()->willReturn($pagesCollection);

$twig->render('@SyliusCmsPlugin/Shop/ContentElement/index.html.twig', [
'content_element' => '@SyliusCmsPlugin/Shop/ContentElement/_pages_collection.html.twig',
'content_element' => $template,
'collection' => $pagesCollection,
])->willReturn('rendered_output');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use PhpSpec\ObjectBehavior;
use Sylius\CmsPlugin\Entity\ContentConfigurationInterface;
use Sylius\CmsPlugin\Form\Type\ContentElements\ProductsCarouselByTaxonContentElementType;
use Sylius\CmsPlugin\Renderer\ContentElement\ContentElementRendererInterface;
use Sylius\CmsPlugin\Renderer\ContentElement\AbstractContentElement;
use Sylius\CmsPlugin\Renderer\ContentElement\ProductsCarouselByTaxonContentElementRenderer;
use Sylius\Component\Core\Model\Product;
use Sylius\Component\Core\Model\TaxonInterface;
Expand All @@ -17,19 +17,15 @@

final class ProductsCarouselByTaxonContentElementRendererSpec extends ObjectBehavior
{
public function let(Environment $twig, ProductRepositoryInterface $productRepository, TaxonRepositoryInterface $taxonRepository): void
public function let(ProductRepositoryInterface $productRepository, TaxonRepositoryInterface $taxonRepository): void
{
$this->beConstructedWith($twig, $productRepository, $taxonRepository);
$this->beConstructedWith($productRepository, $taxonRepository);
}

public function it_is_initializable(): void
{
$this->shouldHaveType(ProductsCarouselByTaxonContentElementRenderer::class);
}

public function it_implements_content_element_renderer_interface(): void
{
$this->shouldImplement(ContentElementRendererInterface::class);
$this->shouldBeAnInstanceOf(AbstractContentElement::class);
}

public function it_supports_products_carousel_by_taxon_content_element_type(ContentConfigurationInterface $contentConfiguration): void
Expand All @@ -53,6 +49,10 @@ public function it_renders_products_carousel_by_taxon_content_element(
Product $product1,
Product $product2,
): void {
$template = 'custom_template';
$this->setTemplate($template);
$this->setTwigEnvironment($twig);

$contentConfiguration->getConfiguration()->willReturn([
'products_carousel_by_taxon' => 'taxon_code',
]);
Expand All @@ -61,7 +61,7 @@ public function it_renders_products_carousel_by_taxon_content_element(
$productRepository->findByTaxon($taxon)->willReturn([$product1, $product2]);

$twig->render('@SyliusCmsPlugin/Shop/ContentElement/index.html.twig', [
'content_element' => '@SyliusCmsPlugin/Shop/ContentElement/_products_carousel.html.twig',
'content_element' => $template,
'products' => [$product1, $product2],
])->willReturn('rendered template');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,23 @@
use PhpSpec\ObjectBehavior;
use Sylius\CmsPlugin\Entity\ContentConfigurationInterface;
use Sylius\CmsPlugin\Form\Type\ContentElements\ProductsCarouselContentElementType;
use Sylius\CmsPlugin\Renderer\ContentElement\ContentElementRendererInterface;
use Sylius\CmsPlugin\Renderer\ContentElement\AbstractContentElement;
use Sylius\CmsPlugin\Renderer\ContentElement\ProductsCarouselContentElementRenderer;
use Sylius\Component\Core\Model\Product;
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
use Twig\Environment;

final class ProductsCarouselContentElementRendererSpec extends ObjectBehavior
{
public function let(Environment $twig, ProductRepositoryInterface $productRepository): void
public function let(ProductRepositoryInterface $productRepository): void
{
$this->beConstructedWith($twig, $productRepository);
$this->beConstructedWith($productRepository);
}

public function it_is_initializable(): void
{
$this->shouldHaveType(ProductsCarouselContentElementRenderer::class);
}

public function it_implements_content_element_renderer_interface(): void
{
$this->shouldImplement(ContentElementRendererInterface::class);
$this->shouldBeAnInstanceOf(AbstractContentElement::class);
}

public function it_supports_products_carousel_content_element_type(ContentConfigurationInterface $contentConfiguration): void
Expand All @@ -49,14 +45,18 @@ public function it_renders_products_carousel_content_element(
Product $product1,
Product $product2,
): void {
$template = 'custom_template';
$this->setTemplate($template);
$this->setTwigEnvironment($twig);

$contentConfiguration->getConfiguration()->willReturn([
'products_carousel' => ['products' => ['code1', 'code2']],
]);

$productRepository->findBy(['code' => ['code1', 'code2']])->willReturn([$product1, $product2]);

$twig->render('@SyliusCmsPlugin/Shop/ContentElement/index.html.twig', [
'content_element' => '@SyliusCmsPlugin/Shop/ContentElement/_products_carousel.html.twig',
'content_element' => $template,
'products' => [$product1, $product2],
])->willReturn('rendered template');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use PhpSpec\ObjectBehavior;
use Sylius\CmsPlugin\Entity\ContentConfigurationInterface;
use Sylius\CmsPlugin\Form\Type\ContentElements\ProductsGridByTaxonContentElementType;
use Sylius\CmsPlugin\Renderer\ContentElement\ContentElementRendererInterface;
use Sylius\CmsPlugin\Renderer\ContentElement\AbstractContentElement;
use Sylius\CmsPlugin\Renderer\ContentElement\ProductsGridByTaxonContentElementRenderer;
use Sylius\Component\Core\Model\Product;
use Sylius\Component\Core\Model\TaxonInterface;
Expand All @@ -17,19 +17,15 @@

final class ProductsGridByTaxonContentElementRendererSpec extends ObjectBehavior
{
public function let(Environment $twig, ProductRepositoryInterface $productRepository, TaxonRepositoryInterface $taxonRepository): void
public function let(ProductRepositoryInterface $productRepository, TaxonRepositoryInterface $taxonRepository): void
{
$this->beConstructedWith($twig, $productRepository, $taxonRepository);
$this->beConstructedWith($productRepository, $taxonRepository);
}

public function it_is_initializable(): void
{
$this->shouldHaveType(ProductsGridByTaxonContentElementRenderer::class);
}

public function it_implements_content_element_renderer_interface(): void
{
$this->shouldImplement(ContentElementRendererInterface::class);
$this->shouldBeAnInstanceOf(AbstractContentElement::class);
}

public function it_supports_products_grid_by_taxon_content_element_type(ContentConfigurationInterface $contentConfiguration): void
Expand All @@ -53,6 +49,10 @@ public function it_renders_products_grid_by_taxon_content_element(
Product $product1,
Product $product2,
): void {
$template = 'custom_template';
$this->setTemplate($template);
$this->setTwigEnvironment($twig);

$contentConfiguration->getConfiguration()->willReturn([
'products_grid_by_taxon' => 'taxon_code',
]);
Expand All @@ -61,7 +61,7 @@ public function it_renders_products_grid_by_taxon_content_element(
$productRepository->findByTaxon($taxon)->willReturn([$product1, $product2]);

$twig->render('@SyliusCmsPlugin/Shop/ContentElement/index.html.twig', [
'content_element' => '@SyliusCmsPlugin/Shop/ContentElement/_products_grid.html.twig',
'content_element' => $template,
'products' => [$product1, $product2],
])->willReturn('rendered template');

Expand Down
Loading
Loading