Skip to content

Commit 12c9ef2

Browse files
committedApr 30, 2021
Render ManyToMany localizable Akeneo attributes on PDP
1 parent c5d25a6 commit 12c9ef2

File tree

2 files changed

+138
-0
lines changed

2 files changed

+138
-0
lines changed
 
+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<?php
2+
3+
namespace Oro\Bundle\AkeneoBundle\Layout\Block\Type;
4+
5+
use Oro\Bundle\EntityConfigBundle\Attribute\Entity\AttributeFamily;
6+
use Oro\Bundle\EntityConfigBundle\Layout\AttributeRenderRegistry;
7+
use Oro\Bundle\EntityConfigBundle\Layout\Mapper\AttributeBlockTypeMapperInterface;
8+
use Oro\Bundle\EntityConfigBundle\Manager\AttributeManager;
9+
use Oro\Bundle\EntityExtendBundle\Extend\RelationType;
10+
use Oro\Bundle\LocaleBundle\Entity\LocalizedFallbackValue;
11+
use Oro\Component\Layout\Block\OptionsResolver\OptionsResolver;
12+
use Oro\Component\Layout\Block\Type\AbstractContainerType;
13+
use Oro\Component\Layout\Block\Type\Options;
14+
use Oro\Component\Layout\BlockBuilderInterface;
15+
use Oro\Component\Layout\BlockInterface;
16+
use Oro\Component\Layout\BlockView;
17+
18+
/**
19+
* Layout block type representing group of Akeneo localizable attributes.
20+
*/
21+
class AttributeGroupType extends AbstractContainerType
22+
{
23+
/** @var AttributeRenderRegistry */
24+
private $attributeRenderRegistry;
25+
26+
/** @var AttributeBlockTypeMapperInterface */
27+
private $blockTypeMapper;
28+
29+
/** @var AttributeManager */
30+
private $attributeManager;
31+
32+
/** @var AbstractContainerType */
33+
private $abstractContainerType;
34+
35+
public function __construct(
36+
AttributeRenderRegistry $attributeRenderRegistry,
37+
AttributeManager $attributeManager,
38+
AttributeBlockTypeMapperInterface $blockTypeMapper,
39+
AbstractContainerType $abstractContainerType
40+
) {
41+
$this->attributeRenderRegistry = $attributeRenderRegistry;
42+
$this->attributeManager = $attributeManager;
43+
$this->blockTypeMapper = $blockTypeMapper;
44+
$this->abstractContainerType = $abstractContainerType;
45+
}
46+
47+
public function buildBlock(BlockBuilderInterface $builder, Options $options)
48+
{
49+
$this->abstractContainerType->buildBlock($builder, $options);
50+
51+
/** @var AttributeFamily $attributeFamily */
52+
$attributeFamily = $options['attribute_family'];
53+
$code = $options['group'];
54+
$entityValue = $options->get('entity', false);
55+
$attributeGroup = $attributeFamily->getAttributeGroup($code);
56+
57+
if (null === $attributeGroup) {
58+
return;
59+
}
60+
61+
$excludeFromRest = $options['exclude_from_rest'];
62+
63+
$options['visible'] = $attributeGroup->getIsVisible();
64+
65+
if ($excludeFromRest) {
66+
$this->attributeRenderRegistry->setGroupRendered($attributeFamily, $attributeGroup);
67+
}
68+
69+
$layoutManipulator = $builder->getLayoutManipulator();
70+
$attributeGroupBlockId = $builder->getId();
71+
$attributes = $this->attributeManager->getAttributesByGroup($attributeGroup);
72+
foreach ($attributes as $attribute) {
73+
if ($this->attributeRenderRegistry->isAttributeRendered($attributeFamily, $attribute->getFieldName())) {
74+
continue;
75+
}
76+
77+
if ($attribute->getType() !== RelationType::MANY_TO_MANY) {
78+
continue;
79+
}
80+
81+
if (($attribute->toArray('extend')['target_entity'] ?? null) !== LocalizedFallbackValue::class) {
82+
continue;
83+
}
84+
85+
if (($attribute->toArray('importexport')['source'] ?? null) !== 'akeneo') {
86+
continue;
87+
}
88+
89+
$fieldName = $attribute->getFieldName();
90+
$blockType = $this->blockTypeMapper->getBlockType($attribute);
91+
$layoutManipulator->add(
92+
$this->getAttributeBlockName($fieldName, $blockType, $attributeGroupBlockId),
93+
$attributeGroupBlockId,
94+
$blockType,
95+
array_merge(
96+
[
97+
'entity' => $entityValue,
98+
'fieldName' => $attribute->getFieldName(),
99+
'className' => $attribute->getEntity()->getClassName(),
100+
],
101+
$options['attribute_options']->toArray()
102+
)
103+
);
104+
}
105+
}
106+
107+
public function buildView(BlockView $view, BlockInterface $block, Options $options)
108+
{
109+
$this->abstractContainerType->buildView($view, $block, $options);
110+
}
111+
112+
private function getAttributeBlockName($field_name, $blockType, $attributeGroupBlockId)
113+
{
114+
return sprintf('%s_%s_%s', $attributeGroupBlockId, $blockType, $field_name);
115+
}
116+
117+
public function configureOptions(OptionsResolver $resolver)
118+
{
119+
$this->abstractContainerType->configureOptions($resolver);
120+
}
121+
122+
public function getName()
123+
{
124+
return $this->abstractContainerType->getName();
125+
}
126+
}

‎Resources/config/services.yml

+12
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,18 @@ services:
182182
- '@oro_akeneo.layout.data_provider.file_applications.inner'
183183
- '@oro_entity_config.provider.attribute'
184184

185+
oro_akeneo.layout.block_type.attribute_group:
186+
class: 'Oro\Bundle\AkeneoBundle\Layout\Block\Type\AttributeGroupType'
187+
decorates: oro_entity_config.block_type.attribute_group
188+
decoration_priority: -255
189+
arguments:
190+
- '@oro_entity_config.attribute_render_registry'
191+
- '@oro_entity_config.manager.attribute_manager'
192+
- '@oro_entity_config.layout.chain_attribute_block_type_mapper'
193+
- '@oro_akeneo.layout.block_type.attribute_group.inner'
194+
tags:
195+
- { name: layout.block_type, alias: attribute_group }
196+
185197
oro_akeneo.product_variant.type_handler.string_type_handle:
186198
class: 'Oro\Bundle\AkeneoBundle\ProductVariant\TypeHandler\StringTypeHandler'
187199
arguments:

0 commit comments

Comments
 (0)