forked from Sylius/CmsPlugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContentElementsCollectionFormComponentTrait.php
More file actions
127 lines (102 loc) · 3.77 KB
/
Copy pathContentElementsCollectionFormComponentTrait.php
File metadata and controls
127 lines (102 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
/*
* This file is part of the Sylius CMS Plugin package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Sylius\CmsPlugin\Twig\Component\Trait;
use Sylius\Bundle\UiBundle\Twig\Component\LiveCollectionTrait;
use Sylius\CmsPlugin\Entity\TemplateInterface;
use Sylius\CmsPlugin\Form\Type\Translation\ContentConfigurationTranslationsType;
use Sylius\CmsPlugin\Repository\TemplateRepositoryInterface;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Symfony\UX\LiveComponent\Attribute\LiveAction;
use Symfony\UX\LiveComponent\Attribute\LiveArg;
use Symfony\UX\LiveComponent\ComponentWithFormTrait;
/**
* @mixin ComponentWithFormTrait
* @mixin LiveCollectionTrait
*
* @see ContentConfigurationTranslationsType
*/
trait ContentElementsCollectionFormComponentTrait
{
/** @var TemplateRepositoryInterface<TemplateInterface> */
protected TemplateRepositoryInterface $templateRepository;
/** @var array<int|string, TemplateInterface> */
protected array $templatesCache = [];
#[LiveAction]
public function moveCollectionItem(
PropertyAccessorInterface $propertyAccessor,
#[LiveArg]
string $name,
#[LiveArg]
int $index,
#[LiveArg]
string $direction,
): void {
if (null === $this->formName) {
return;
}
$propertyPath = $this->fieldNameToPropertyPath($name, $this->formName);
$data = $propertyAccessor->getValue($this->formValues, $propertyPath);
if (!\is_array($data)) {
return;
}
$keys = array_keys($data);
$currentPos = array_search($index, $keys, true);
if (false === $currentPos) {
return;
}
$swapPos = 'up' === $direction ? $currentPos - 1 : $currentPos + 1;
if ($swapPos < 0 || $swapPos >= \count($keys)) {
return;
}
$swapKey = $keys[$swapPos];
[$data[$index], $data[$swapKey]] = [$data[$swapKey], $data[$index]];
$propertyAccessor->setValue($this->formValues, $propertyPath, $data);
}
#[LiveAction]
public function applyContentTemplate(#[LiveArg] string $localeCode): void
{
$templateId = $this->formValues['contentElements'][$localeCode]['template'] ?? null;
$template = $this->getTemplateElements($templateId);
if (null === $template) {
return;
}
$this->populateElements($localeCode, $template);
}
/** @param TemplateRepositoryInterface<TemplateInterface> $templateRepository */
protected function initializeTemplateRepository(TemplateRepositoryInterface $templateRepository): void
{
$this->templateRepository = $templateRepository;
}
protected function populateElements(string $locale, ?TemplateInterface $template): void
{
if (null === $template) {
return;
}
$this->formValues['contentElements'][$locale]['contentElements'] = [];
foreach ($template->getContentElements() as $element) {
$this->formValues['contentElements'][$locale]['contentElements'][] = [
'type' => $element['type'],
];
}
$this->submitForm();
}
protected function getTemplateElements(mixed $templateId): ?TemplateInterface
{
if (null !== $templateId && '' !== $templateId && !isset($this->templatesCache[$templateId])) {
$template = $this->templateRepository->find($templateId);
if (null === $template) {
return null;
}
$this->templatesCache[$templateId] = $template;
}
return $this->templatesCache[$templateId] ?? null;
}
}