/
AttributeFormViewListener.php
202 lines (168 loc) · 5.84 KB
/
AttributeFormViewListener.php
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
namespace Oro\Bundle\ProductBundle\EventListener;
use Oro\Bundle\CMSBundle\DBAL\Types\WYSIWYGType;
use Oro\Bundle\EntityConfigBundle\Attribute\Entity\AttributeGroup;
use Oro\Bundle\EntityConfigBundle\Entity\FieldConfigModel;
use Oro\Bundle\EntityConfigBundle\EventListener\AttributeFormViewListener as BaseAttributeFormViewListener;
use Oro\Bundle\EntityConfigBundle\Manager\AttributeManager;
use Oro\Bundle\EntityConfigBundle\Provider\ConfigProvider;
use Oro\Bundle\ProductBundle\Entity\Product;
use Oro\Bundle\UIBundle\Event\BeforeListRenderEvent;
use Oro\Bundle\UIBundle\View\ScrollData;
use Symfony\Component\Form\FormView;
use Symfony\Contracts\Translation\TranslatorInterface;
use Twig\Environment;
/**
* This class allows to restrict moving of attributes
*/
class AttributeFormViewListener extends BaseAttributeFormViewListener
{
/** @var int */
private const DEFAULT_PRIORITY = 500;
/**
* @internal
*/
const EVENT_TYPE_VIEW = 'view';
/**
* @var ConfigProvider
*/
private $entityConfigProvider;
/**
* @var TranslatorInterface
*/
private $translator;
/**
* @var array
*/
private $fieldsRestrictedToMove = [
'inventory_status',
'images',
'productPriceAttributesPrices',
'shortDescriptions',
'descriptions',
];
/**
* This property used to determine type of event inside moveFieldToBlock.
* It's safe because it wll be cleared after event processing
*
* @var string
*/
private $eventType;
public function __construct(
AttributeManager $attributeManager,
ConfigProvider $entityConfigProvider,
TranslatorInterface $translator
) {
parent::__construct($attributeManager);
$this->entityConfigProvider = $entityConfigProvider;
$this->translator = $translator;
}
/**
* {@inheritDoc}
*/
public function onViewList(BeforeListRenderEvent $event)
{
$this->eventType = self::EVENT_TYPE_VIEW;
parent::onViewList($event);
$this->eventType = null;
}
/**
* {@inheritDoc}
*/
protected function moveFieldToBlock(ScrollData $scrollData, $fieldName, $blockId)
{
if ($this->eventType === self::EVENT_TYPE_VIEW) {
if (in_array($fieldName, $this->getRestrictedToMoveFields(), true)) {
return;
}
}
parent::moveFieldToBlock($scrollData, $fieldName, $blockId);
}
/**
* @return array
*/
protected function getRestrictedToMoveFields()
{
return $this->fieldsRestrictedToMove;
}
protected function addNotEmptyGroupBlocks(ScrollData $scrollData, array $groups)
{
parent::addNotEmptyGroupBlocks($scrollData, $groups);
foreach ($groups as $group) {
if (empty($group['attributes'])) {
continue;
}
/** @var AttributeGroup $currentGroup */
$currentGroup = $group['group'];
$block = $scrollData->getBlock($currentGroup->getCode());
$priority = $block[ScrollData::PRIORITY] ?? self::DEFAULT_PRIORITY;
/** @var FieldConfigModel $attribute */
foreach ($group['attributes'] as $attribute) {
if (!$this->isSeparateGroup($attribute->getType())) {
continue;
}
$config = $this->entityConfigProvider->getConfig(Product::class, $attribute->getFieldName());
$scrollData->addNamedBlock(
$attribute->getFieldName(),
$this->translator->trans((string) $config->get('label')),
++$priority
);
}
}
}
/**
* {@inheritdoc}
*/
protected function addAttributeEditBlocks(BeforeListRenderEvent $event, AttributeGroup $group, array $attributes)
{
parent::addAttributeEditBlocks($event, $group, $attributes);
foreach ($attributes as $attribute) {
if (!$this->isSeparateGroup($attribute->getType())) {
continue;
}
$this->moveFieldToBlock($event->getScrollData(), $attribute->getFieldName(), $attribute->getFieldName());
}
}
/**
* {@inheritdoc}
*/
protected function renderAttributeEditData(Environment $twig, FormView $attributeView, FieldConfigModel $attribute)
{
return $this->isSeparateGroup($attribute->getType())
? $twig->render('@OroEntityConfig/Attribute/widget.html.twig', ['child' => $attributeView])
: parent::renderAttributeEditData($twig, $attributeView, $attribute);
}
/**
* {@inheritdoc}
*/
protected function addAttributeViewBlocks(BeforeListRenderEvent $event, AttributeGroup $group, array $attributes)
{
parent::addAttributeViewBlocks($event, $group, $attributes);
foreach ($attributes as $attribute) {
if (!$this->isSeparateGroup($attribute->getType())) {
continue;
}
$this->moveFieldToBlock($event->getScrollData(), $attribute->getFieldName(), $attribute->getFieldName());
}
}
/**
* @param Environment $twig
* @param object $entity
* @param FieldConfigModel $attribute
* @return string
*/
protected function renderAttributeViewData(Environment $twig, $entity, FieldConfigModel $attribute)
{
if ($this->isSeparateGroup($attribute->getType())) {
return $twig->render(
'@OroEntityConfig/Attribute/attributeCollapsibleView.html.twig',
['entity' => $entity, 'field' => $attribute]
);
}
return parent::renderAttributeViewData($twig, $entity, $attribute);
}
protected function isSeparateGroup(?string $type): bool
{
return in_array((string)$type, [WYSIWYGType::TYPE], true);
}
}