Skip to content

Commit

Permalink
feat: display template location for all bricks
Browse files Browse the repository at this point in the history
  • Loading branch information
jdreesen committed Oct 25, 2024
1 parent a9a9633 commit e56e392
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 2 deletions.
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
}
},
"autoload-dev": {
"classmap": [
"tests/app/TestKernel.php"
],
"psr-4": {
"Neusta\\Pimcore\\AreabrickConfigBundle\\Tests\\": "tests/"
}
Expand Down
2 changes: 1 addition & 1 deletion config/pimcore/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ neusta_converter:
neusta_pimcore_areabrick_config.brick.converter:
target: Neusta\Pimcore\AreabrickConfigBundle\Bricks\Model\Brick
populators:
- Neusta\Pimcore\AreabrickConfigBundle\Bricks\Populator\BrickTemplateLocationPopulator
- Neusta\Pimcore\AreabrickConfigBundle\Bricks\Populator\BrickPagePopulator
properties:
id: ~
name: ~
version: ~
description: ~
template: ~

neusta_pimcore_areabrick_config.page.converter:
target: Neusta\Pimcore\AreabrickConfigBundle\Bricks\Model\Page
Expand Down
2 changes: 1 addition & 1 deletion config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ services:
$brickConverter: '@neusta_pimcore_areabrick_config.brick.converter'
tags: [ 'controller.service_arguments' ]

Neusta\Pimcore\AreabrickConfigBundle\Bricks\Populator\BrickPageEditModeUrlPopulator: ~
Neusta\Pimcore\AreabrickConfigBundle\Bricks\Populator\BrickTemplateLocationPopulator: ~

Neusta\Pimcore\AreabrickConfigBundle\Bricks\Populator\BrickPagePopulator:
arguments:
Expand Down
29 changes: 29 additions & 0 deletions src/Bricks/Populator/BrickTemplateLocationPopulator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
declare(strict_types=1);

namespace Neusta\Pimcore\AreabrickConfigBundle\Bricks\Populator;

use Neusta\ConverterBundle\Converter\Context\GenericContext;
use Neusta\ConverterBundle\Populator;
use Neusta\Pimcore\AreabrickConfigBundle\Bricks\Model\Brick;
use Pimcore\Document\Editable\EditableHandler;
use Pimcore\Extension\Document\Areabrick\AbstractAreabrick;

/**
* @implements Populator<AbstractAreabrick, Brick, GenericContext>
*/
final class BrickTemplateLocationPopulator implements Populator
{
private readonly \Closure $resolveTemplate;

public function __construct(EditableHandler $editableHandler)
{
$this->resolveTemplate = (new \ReflectionMethod($editableHandler, 'resolveBrickTemplate'))->getClosure($editableHandler);
}

public function populate(object $target, object $source, ?object $ctx = null): void
{
// Todo: remove the second parameter when Pimcore 10 support is dropped
$target->template = ($this->resolveTemplate)($source, 'view');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
declare(strict_types=1);

namespace Neusta\Pimcore\AreabrickConfigBundle\Tests\Integration\Bricks\Populator;

use Neusta\Pimcore\AreabrickConfigBundle\Bricks\Model\Brick;
use Neusta\Pimcore\AreabrickConfigBundle\Bricks\Populator\BrickTemplateLocationPopulator;
use Pimcore\Extension\Document\Areabrick\AreabrickManagerInterface;
use Pimcore\Test\KernelTestCase;

final class BrickTemplateLocationPopulatorTest extends KernelTestCase
{
/**
* @test
*/
public function itPopulatesCustomTemplates(): void
{
$container = self::getContainer();
$populator = $container->get(BrickTemplateLocationPopulator::class);
$source = $container->get(AreabrickManagerInterface::class)->getBrick('custom-template');
$target = new Brick();

$populator->populate($target, $source);

self::assertSame('custom-template-areabrick/index.html.twig', $target->template);
}

/**
* @test
*/
public function itPopulatesDefaultTemplateLocations(): void
{
$container = self::getContainer();
$populator = $container->get(BrickTemplateLocationPopulator::class);
$source = $container->get(AreabrickManagerInterface::class)->getBrick('global-template-location');
$target = new Brick();

$populator->populate($target, $source);

self::assertSame('areas/global-template-location/view.html.twig', $target->template);
}
}
Empty file removed tests/app/config/.gitkeep
Empty file.
8 changes: 8 additions & 0 deletions tests/app/config/services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
services:
Neusta\Pimcore\AreabrickConfigBundle\Tests\app\src\Document\Areabrick\CustomTemplate:
tags:
- { name: pimcore.area.brick, id: custom-template }

Neusta\Pimcore\AreabrickConfigBundle\Tests\app\src\Document\Areabrick\GlobalTemplateLocation:
tags:
- { name: pimcore.area.brick, id: global-template-location }
24 changes: 24 additions & 0 deletions tests/app/src/Document/Areabrick/CustomTemplate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
declare(strict_types=1);

namespace Neusta\Pimcore\AreabrickConfigBundle\Tests\app\src\Document\Areabrick;

use Pimcore\Extension\Document\Areabrick\AbstractAreabrick;

final class CustomTemplate extends AbstractAreabrick
{
public function getTemplate(): ?string
{
return 'custom-template-areabrick/index.html.twig';
}

public function getTemplateLocation(): string
{
return '';
}

public function getTemplateSuffix(): string
{
return '';
}
}
24 changes: 24 additions & 0 deletions tests/app/src/Document/Areabrick/GlobalTemplateLocation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
declare(strict_types=1);

namespace Neusta\Pimcore\AreabrickConfigBundle\Tests\app\src\Document\Areabrick;

use Pimcore\Extension\Document\Areabrick\AbstractAreabrick;

final class GlobalTemplateLocation extends AbstractAreabrick
{
public function getTemplate(): ?string
{
return null;
}

public function getTemplateLocation(): string
{
return self::TEMPLATE_LOCATION_GLOBAL;
}

public function getTemplateSuffix(): string
{
return self::TEMPLATE_SUFFIX_TWIG;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Global Template Location Areabrick
1 change: 1 addition & 0 deletions tests/app/templates/custom-template-areabrick/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Custom Template Areabrick

0 comments on commit e56e392

Please sign in to comment.