Skip to content

Commit

Permalink
OP-515: Change service definition for content elements
Browse files Browse the repository at this point in the history
  • Loading branch information
jkindly committed Sep 10, 2024
1 parent e94dcdc commit 660e8cd
Show file tree
Hide file tree
Showing 17 changed files with 187 additions and 135 deletions.
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
57 changes: 57 additions & 0 deletions src/DependencyInjection/Compiler/ContentElementPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace Sylius\CmsPlugin\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

final class ContentElementPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
{
foreach ($container->findTaggedServiceIds('sylius_cms.content_element') as $id => $attributes) {
if (!isset($attributes[0]['template'])) {
throw new \InvalidArgumentException(
sprintf('Tagged content element "%s" needs to have `template` attribute.', $id),
);
}

$definition = $container->getDefinition($id);
$definition->addMethodCall('setTemplate', [$attributes[0]['template']]);
$definition->addMethodCall('setTwigEnvironment', [new Reference('twig')]);

if (isset($attributes[0]['form_type'])) {
$this->registerFormTypeService($container, $attributes[0]['form_type']);
}
}
}

private function retrieveElementNameConstantFromFormType(string $formType): string
{
if (!class_exists($formType)) {
throw new \InvalidArgumentException(sprintf('Form type "%s" does not exist.', $formType));
}

if (!defined($formType . '::TYPE')) {
throw new \InvalidArgumentException(sprintf('Form type "%s" needs to have "TYPE" constant.', $formType));
}

return constant(sprintf('%s::TYPE', $formType));
}

private function registerFormTypeService(ContainerBuilder $container, string $formType): void
{
$elementName = $this->retrieveElementNameConstantFromFormType($formType);
$formTypeDefinition = new Definition($formType);
$formTypeDefinition->addTag('form.type');
$formTypeDefinition->addTag('sylius_cms.content_elements.type', [
'key' => $elementName,
]);

$container->setDefinition('sylius_cms.form.type.content_element.' . $elementName, $formTypeDefinition);
}
}
24 changes: 24 additions & 0 deletions src/Renderer/ContentElement/AbstractContentElement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Sylius\CmsPlugin\Renderer\ContentElement;

use Twig\Environment;

abstract class AbstractContentElement implements ContentElementRendererInterface
{
protected string $template;

protected Environment $twig;

public function setTemplate(string $template): void
{
$this->template = $template;
}

public function setTwigEnvironment(Environment $twig): void
{
$this->twig = $twig;
}
}
9 changes: 2 additions & 7 deletions src/Renderer/ContentElement/HeadingContentElementRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,9 @@

use Sylius\CmsPlugin\Entity\ContentConfigurationInterface;
use Sylius\CmsPlugin\Form\Type\ContentElements\HeadingContentElementType;
use Twig\Environment;

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

public function supports(ContentConfigurationInterface $contentConfiguration): bool
{
return HeadingContentElementType::TYPE === $contentConfiguration->getType();
Expand All @@ -26,7 +21,7 @@ public function render(ContentConfigurationInterface $contentConfiguration): str
$headingContent = $configuration['heading'];

return $this->twig->render('@SyliusCmsPlugin/Shop/ContentElement/index.html.twig', [
'content_element' => '@SyliusCmsPlugin/Shop/ContentElement/_heading.html.twig',
'content_element' => $this->template,
'heading_type' => $headingType,
'heading_content' => $headingContent,
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@
use Sylius\CmsPlugin\Form\Type\ContentElements\MultipleMediaContentElementType;
use Sylius\CmsPlugin\Repository\MediaRepositoryInterface;
use Sylius\CmsPlugin\Twig\Runtime\RenderMediaRuntimeInterface;
use Twig\Environment;

final class MultipleMediaContentElementRenderer implements ContentElementRendererInterface
final class MultipleMediaContentElementRenderer extends AbstractContentElement
{
public function __construct(
private Environment $twig,
private RenderMediaRuntimeInterface $renderMediaRuntime,
private MediaRepositoryInterface $mediaRepository,
) {
Expand Down Expand Up @@ -46,7 +44,7 @@ public function render(ContentConfigurationInterface $contentConfiguration): str
}

return $this->twig->render('@SyliusCmsPlugin/Shop/ContentElement/index.html.twig', [
'content_element' => '@SyliusCmsPlugin/Shop/ContentElement/_multiple_media.html.twig',
'content_element' => $this->template,
'media' => $media,
]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@
use Sylius\CmsPlugin\Entity\ContentConfigurationInterface;
use Sylius\CmsPlugin\Form\Type\ContentElements\PagesCollectionContentElementType;
use Sylius\CmsPlugin\Repository\CollectionRepositoryInterface;
use Twig\Environment;

final class PagesCollectionContentElementRenderer implements ContentElementRendererInterface
final class PagesCollectionContentElementRenderer extends AbstractContentElement
{
public function __construct(
private Environment $twig,
private CollectionRepositoryInterface $collectionRepository,
) {
}
Expand All @@ -34,7 +32,7 @@ public function render(ContentConfigurationInterface $contentConfiguration): str
$collection = $this->collectionRepository->findOneBy(['code' => $code]);

return $this->twig->render('@SyliusCmsPlugin/Shop/ContentElement/index.html.twig', [
'content_element' => '@SyliusCmsPlugin/Shop/ContentElement/_pages_collection.html.twig',
'content_element' => $this->template,
'collection' => $collection?->getPages(),
]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@
use Sylius\Component\Core\Model\TaxonInterface;
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface;
use Twig\Environment;

final class ProductsCarouselByTaxonContentElementRenderer implements ContentElementRendererInterface
final class ProductsCarouselByTaxonContentElementRenderer extends AbstractContentElement
{
public function __construct(
private Environment $twig,
private ProductRepositoryInterface $productRepository,
private TaxonRepositoryInterface $taxonRepository,
) {
Expand All @@ -38,7 +36,7 @@ public function render(ContentConfigurationInterface $contentConfiguration): str
$products = $this->productRepository->findByTaxon($taxon);

return $this->twig->render('@SyliusCmsPlugin/Shop/ContentElement/index.html.twig', [
'content_element' => '@SyliusCmsPlugin/Shop/ContentElement/_products_carousel.html.twig',
'content_element' => $this->template,
'products' => $products,
]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@
use Sylius\CmsPlugin\Entity\ContentConfigurationInterface;
use Sylius\CmsPlugin\Form\Type\ContentElements\ProductsCarouselContentElementType;
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
use Twig\Environment;

final class ProductsCarouselContentElementRenderer implements ContentElementRendererInterface
final class ProductsCarouselContentElementRenderer extends AbstractContentElement
{
public function __construct(
private Environment $twig,
private ProductRepositoryInterface $productRepository,
) {
}
Expand All @@ -32,7 +30,7 @@ public function render(ContentConfigurationInterface $contentConfiguration): str
}

return $this->twig->render('@SyliusCmsPlugin/Shop/ContentElement/index.html.twig', [
'content_element' => '@SyliusCmsPlugin/Shop/ContentElement/_products_carousel.html.twig',
'content_element' => $this->template,
'products' => $products,
]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@
use Sylius\Component\Core\Model\TaxonInterface;
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface;
use Twig\Environment;

final class ProductsGridByTaxonContentElementRenderer implements ContentElementRendererInterface
final class ProductsGridByTaxonContentElementRenderer extends AbstractContentElement
{
public function __construct(
private Environment $twig,
private ProductRepositoryInterface $productRepository,
private TaxonRepositoryInterface $taxonRepository,
) {
Expand All @@ -38,7 +36,7 @@ public function render(ContentConfigurationInterface $contentConfiguration): str
$products = $this->productRepository->findByTaxon($taxon);

return $this->twig->render('@SyliusCmsPlugin/Shop/ContentElement/index.html.twig', [
'content_element' => '@SyliusCmsPlugin/Shop/ContentElement/_products_grid.html.twig',
'content_element' => $this->template,
'products' => $products,
]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@
use Sylius\CmsPlugin\Entity\ContentConfigurationInterface;
use Sylius\CmsPlugin\Form\Type\ContentElements\ProductsGridContentElementType;
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
use Twig\Environment;

final class ProductsGridContentElementRenderer implements ContentElementRendererInterface
final class ProductsGridContentElementRenderer extends AbstractContentElement
{
public function __construct(
private Environment $twig,
private ProductRepositoryInterface $productRepository,
) {
}
Expand All @@ -29,7 +27,7 @@ public function render(ContentConfigurationInterface $contentConfiguration): str
$products = $this->productRepository->findBy(['code' => $productsCodes]);

return $this->twig->render('@SyliusCmsPlugin/Shop/ContentElement/index.html.twig', [
'content_element' => '@SyliusCmsPlugin/Shop/ContentElement/_products_grid.html.twig',
'content_element' => $this->template,
'products' => $products,
]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@
use Sylius\CmsPlugin\Form\Type\ContentElements\SingleMediaContentElementType;
use Sylius\CmsPlugin\Repository\MediaRepositoryInterface;
use Sylius\CmsPlugin\Twig\Runtime\RenderMediaRuntimeInterface;
use Twig\Environment;

final class SingleMediaContentElementRenderer implements ContentElementRendererInterface
final class SingleMediaContentElementRenderer extends AbstractContentElement
{
public function __construct(
private Environment $twig,
private RenderMediaRuntimeInterface $renderMediaRuntime,
private MediaRepositoryInterface $mediaRepository,
) {
Expand All @@ -37,7 +35,7 @@ public function render(ContentConfigurationInterface $contentConfiguration): str
];

return $this->twig->render('@SyliusCmsPlugin/Shop/ContentElement/index.html.twig', [
'content_element' => '@SyliusCmsPlugin/Shop/ContentElement/_single_media.html.twig',
'content_element' => $this->template,
'media' => $media,
]);
}
Expand Down
9 changes: 2 additions & 7 deletions src/Renderer/ContentElement/SpacerContentElementRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,9 @@

use Sylius\CmsPlugin\Entity\ContentConfigurationInterface;
use Sylius\CmsPlugin\Form\Type\ContentElements\SpacerContentElementType;
use Twig\Environment;

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

public function supports(ContentConfigurationInterface $contentConfiguration): bool
{
return SpacerContentElementType::TYPE === $contentConfiguration->getType();
Expand All @@ -24,7 +19,7 @@ public function render(ContentConfigurationInterface $contentConfiguration): str
$configuration = (int) $contentConfiguration->getConfiguration()['spacer'];

return $this->twig->render('@SyliusCmsPlugin/Shop/ContentElement/index.html.twig', [
'content_element' => '@SyliusCmsPlugin/Shop/ContentElement/_spacer.html.twig',
'content_element' => $this->template,
'spacer_height' => $configuration,
]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@
use Sylius\CmsPlugin\Entity\ContentConfigurationInterface;
use Sylius\CmsPlugin\Form\Type\ContentElements\TaxonsListContentElementType;
use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface;
use Twig\Environment;

final class TaxonsListContentElementRenderer implements ContentElementRendererInterface
final class TaxonsListContentElementRenderer extends AbstractContentElement
{
public function __construct(
private Environment $twig,
private TaxonRepositoryInterface $taxonRepository,
) {
}
Expand All @@ -29,7 +27,7 @@ public function render(ContentConfigurationInterface $contentConfiguration): str
$taxons = $this->taxonRepository->findBy(['code' => $taxonsCodes]);

return $this->twig->render('@SyliusCmsPlugin/Shop/ContentElement/index.html.twig', [
'content_element' => '@SyliusCmsPlugin/Shop/ContentElement/_taxons_list.html.twig',
'content_element' => $this->template,
'taxons' => $taxons,
]);
}
Expand Down
Loading

0 comments on commit 660e8cd

Please sign in to comment.