Skip to content

Commit 455eabb

Browse files
Aleksander Laurowskia.laurowski
authored andcommitted
FFWEB-2443 add layout column to cms export
1 parent 56d4eea commit 455eabb

4 files changed

Lines changed: 217 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# Changelog
2+
## [Unreleased]
3+
### Add
4+
- Export
5+
- Added new field provider `Omikron\FactFinder\Shopware6\Export\Field\Layout` applicable to CMS Export
6+
27
## [v3.0.1] - 2022.03.10
3-
48
### Fix
59
- `Omikron\FactFinder\Shopware6\Subscriber\CategoryView`
610
- fix category path is not encoded correctly

spec/Export/Field/LayoutSpec.php

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace spec\Omikron\FactFinder\Shopware6\Export\Field;
6+
7+
use Omikron\FactFinder\Shopware6\Export\Filter\TextFilter;
8+
use PhpSpec\ObjectBehavior;
9+
use Shopware\Core\Content\Category\CategoryEntity;
10+
use Shopware\Core\Content\Cms\Aggregate\CmsBlock\CmsBlockCollection;
11+
use Shopware\Core\Content\Cms\Aggregate\CmsBlock\CmsBlockEntity;
12+
use Shopware\Core\Content\Cms\Aggregate\CmsSection\CmsSectionCollection;
13+
use Shopware\Core\Content\Cms\Aggregate\CmsSection\CmsSectionEntity;
14+
use Shopware\Core\Content\Cms\Aggregate\CmsSlot\CmsSlotCollection;
15+
use Shopware\Core\Content\Cms\Aggregate\CmsSlot\CmsSlotEntity;
16+
use Shopware\Core\Content\Cms\CmsPageEntity;
17+
18+
class LayoutSpec extends ObjectBehavior
19+
{
20+
public function let(): void
21+
{
22+
$this->beConstructedWith(new TextFilter());
23+
}
24+
25+
public function it_should_strip_html_tags(CategoryEntity $categoryEntity): void
26+
{
27+
$cmsPage = new CmsPageEntity();
28+
$cmsPage->setSections(
29+
new CmsSectionCollection(
30+
[
31+
'1' => $this->createSection(
32+
[
33+
$this->createBlock(
34+
[
35+
$this->createSlot(['content' => ['value' => '<h2>Lorem Ipsum </h2><p>Dolor sit amet</p>']]),
36+
]
37+
),
38+
]
39+
),
40+
]
41+
)
42+
);
43+
$categoryEntity->getCmsPage()->willReturn($cmsPage);
44+
$this->getValue($categoryEntity)->shouldReturn('Lorem Ipsum Dolor sit amet');
45+
}
46+
47+
public function it_should_concatenate_values_from_all_slots_configs(CategoryEntity $categoryEntity): void
48+
{
49+
$cmsPage = new CmsPageEntity();
50+
$cmsPage->setSections(
51+
new CmsSectionCollection(
52+
[
53+
'1' => $this->createSection(
54+
[
55+
$this->createBlock(
56+
[
57+
$this->createSlot(['content' => ['value' => 'I am']]),
58+
], 1
59+
),
60+
],
61+
),
62+
'2' => $this->createSection(
63+
[
64+
$this->createBlock(
65+
[
66+
$this->createSlot(['content' => ['value' => 'concatenated']]),
67+
], 2
68+
),
69+
], 2
70+
),
71+
]
72+
)
73+
);
74+
$categoryEntity->getCmsPage()->willReturn($cmsPage);
75+
$this->getValue($categoryEntity)->shouldReturn('I am concatenated');
76+
}
77+
78+
public function it_should_sort_output_by_position(CategoryEntity $categoryEntity): void
79+
{
80+
$cmsPage = new CmsPageEntity();
81+
$cmsPage->setSections(
82+
new CmsSectionCollection(
83+
[
84+
'1' => $this->createSection(
85+
[
86+
$this->createBlock(
87+
[
88+
$this->createSlot(['content' => ['value' => 'not']]),
89+
], 2
90+
),
91+
$this->createBlock(
92+
[
93+
$this->createSlot(['content' => ['value' => 'Yoda']]),
94+
],
95+
3
96+
),
97+
$this->createBlock(
98+
[
99+
$this->createSlot(['content' => ['value' => 'I am']]),
100+
], 1
101+
),
102+
]
103+
),
104+
]
105+
)
106+
);
107+
108+
$categoryEntity->getCmsPage()->willReturn($cmsPage);
109+
$this->getValue($categoryEntity)->shouldReturn('I am not Yoda');
110+
}
111+
112+
private function createSection(array $blocks, int $position = 1): CmsSectionEntity
113+
{
114+
$section = new CmsSectionEntity();
115+
$section->setId(uniqid());
116+
$section->setPosition($position);
117+
$section->setBlocks(new CmsBlockCollection($blocks));
118+
return $section;
119+
}
120+
121+
private function createBlock(array $slots, int $position = 1): CmsBlockEntity
122+
{
123+
$block = new CmsBlockEntity();
124+
$block->setId(uniqid());
125+
$block->setPosition($position);
126+
$block->setSlots(new CmsSlotCollection($slots));
127+
return $block;
128+
}
129+
130+
private function createSlot(array $config): CmsSlotEntity
131+
{
132+
$slot = new CmsSlotEntity();
133+
$slot->setId(uniqid());
134+
$slot->setSlot(uniqid('slot-'));
135+
$slot->setConfig($config);
136+
return $slot;
137+
}
138+
}

src/Export/ExportCategories.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ private function getCriteria(int $batchSize): Criteria
5454
$criteria->addAssociation('customFields');
5555
$criteria->addAssociation('media');
5656
$criteria->addAssociation('seoUrls');
57+
$criteria->addAssociation('cmsPage.sections');
58+
$criteria->addAssociation('cmsPage.sections.blocks');
59+
$criteria->addAssociation('cmsPage.sections.blocks.slots');
60+
$criteria->addAssociation('slotConfig');
5761
foreach ($this->customAssociations as $association) {
5862
$criteria->addAssociation($association);
5963
}

src/Export/Field/Layout.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Omikron\FactFinder\Shopware6\Export\Field;
6+
7+
use Omikron\FactFinder\Shopware6\Export\Filter\TextFilter;
8+
use Shopware\Core\Content\Category\CategoryEntity;
9+
use Shopware\Core\Content\Cms\Aggregate\CmsBlock\CmsBlockEntity;
10+
use Shopware\Core\Content\Cms\Aggregate\CmsSection\CmsSectionEntity;
11+
use Shopware\Core\Content\Cms\Aggregate\CmsSlot\CmsSlotEntity;
12+
use Shopware\Core\Framework\DataAbstractionLayer\Entity;
13+
use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection;
14+
use function Omikron\FactFinder\Shopware6\Internal\Utils\flatMap;
15+
use function Omikron\FactFinder\Shopware6\Internal\Utils\safeGetByName;
16+
17+
class Layout implements FieldInterface
18+
{
19+
private TextFilter $textFilter;
20+
21+
public function __construct(TextFilter $textFilter)
22+
{
23+
$this->textFilter = $textFilter;
24+
}
25+
26+
public function getName(): string
27+
{
28+
return 'Layout';
29+
}
30+
31+
/**
32+
* @param CategoryEntity $entity
33+
*
34+
* @return string
35+
*/
36+
public function getValue(Entity $entity): string
37+
{
38+
$layout = array_map(
39+
fn (CmsSlotEntity $slot) => safeGetByName(safeGetByName($slot->getConfig(), 'content'), 'value'),
40+
flatMap(
41+
fn (CmsBlockEntity $block): array => $this->toValues($block->getSlots()),
42+
flatMap(
43+
fn (CmsSectionEntity $section): array => $this->toValues($section->getBlocks()),
44+
$this->toValues($entity->getCmsPage()->getSections())
45+
)
46+
)
47+
);
48+
49+
return $this->textFilter->filterValue(implode(' ', array_filter($layout)));
50+
}
51+
52+
public function getCompatibleEntityTypes(): array
53+
{
54+
return [CategoryEntity::class];
55+
}
56+
57+
private function toValues(EntityCollection $collection): array
58+
{
59+
/**
60+
* @param CmsBlockEntity|CmsSectionEntity $current
61+
* @param CmsBlockEntity|CmsSectionEntity $next
62+
*
63+
* @return bool
64+
*/
65+
$sortByPosition = fn ($current, $next): bool => !method_exists($current, 'getPosition') || $current->getPosition() > $next->getPosition();
66+
$collection->sort($sortByPosition);
67+
68+
return array_values($collection->getElements());
69+
}
70+
}

0 commit comments

Comments
 (0)