diff --git a/Layout/Block/Type/AttributeGroupType.php b/Layout/Block/Type/AttributeGroupType.php new file mode 100644 index 00000000..159d8b2c --- /dev/null +++ b/Layout/Block/Type/AttributeGroupType.php @@ -0,0 +1,126 @@ +attributeRenderRegistry = $attributeRenderRegistry; + $this->attributeManager = $attributeManager; + $this->blockTypeMapper = $blockTypeMapper; + $this->abstractContainerType = $abstractContainerType; + } + + public function buildBlock(BlockBuilderInterface $builder, Options $options) + { + $this->abstractContainerType->buildBlock($builder, $options); + + /** @var AttributeFamily $attributeFamily */ + $attributeFamily = $options['attribute_family']; + $code = $options['group']; + $entityValue = $options->get('entity', false); + $attributeGroup = $attributeFamily->getAttributeGroup($code); + + if (null === $attributeGroup) { + return; + } + + $excludeFromRest = $options['exclude_from_rest']; + + $options['visible'] = $attributeGroup->getIsVisible(); + + if ($excludeFromRest) { + $this->attributeRenderRegistry->setGroupRendered($attributeFamily, $attributeGroup); + } + + $layoutManipulator = $builder->getLayoutManipulator(); + $attributeGroupBlockId = $builder->getId(); + $attributes = $this->attributeManager->getAttributesByGroup($attributeGroup); + foreach ($attributes as $attribute) { + if ($this->attributeRenderRegistry->isAttributeRendered($attributeFamily, $attribute->getFieldName())) { + continue; + } + + if ($attribute->getType() !== RelationType::MANY_TO_MANY) { + continue; + } + + if (($attribute->toArray('extend')['target_entity'] ?? null) !== LocalizedFallbackValue::class) { + continue; + } + + if (($attribute->toArray('importexport')['source'] ?? null) !== 'akeneo') { + continue; + } + + $fieldName = $attribute->getFieldName(); + $blockType = $this->blockTypeMapper->getBlockType($attribute); + $layoutManipulator->add( + $this->getAttributeBlockName($fieldName, $blockType, $attributeGroupBlockId), + $attributeGroupBlockId, + $blockType, + array_merge( + [ + 'entity' => $entityValue, + 'fieldName' => $attribute->getFieldName(), + 'className' => $attribute->getEntity()->getClassName(), + ], + $options['attribute_options']->toArray() + ) + ); + } + } + + public function buildView(BlockView $view, BlockInterface $block, Options $options) + { + $this->abstractContainerType->buildView($view, $block, $options); + } + + private function getAttributeBlockName($field_name, $blockType, $attributeGroupBlockId) + { + return sprintf('%s_%s_%s', $attributeGroupBlockId, $blockType, $field_name); + } + + public function configureOptions(OptionsResolver $resolver) + { + $this->abstractContainerType->configureOptions($resolver); + } + + public function getName() + { + return $this->abstractContainerType->getName(); + } +} diff --git a/Resources/config/services.yml b/Resources/config/services.yml index da5ae79a..7b6ff9bc 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -182,6 +182,18 @@ services: - '@oro_akeneo.layout.data_provider.file_applications.inner' - '@oro_entity_config.provider.attribute' + oro_akeneo.layout.block_type.attribute_group: + class: 'Oro\Bundle\AkeneoBundle\Layout\Block\Type\AttributeGroupType' + decorates: oro_entity_config.block_type.attribute_group + decoration_priority: -255 + arguments: + - '@oro_entity_config.attribute_render_registry' + - '@oro_entity_config.manager.attribute_manager' + - '@oro_entity_config.layout.chain_attribute_block_type_mapper' + - '@oro_akeneo.layout.block_type.attribute_group.inner' + tags: + - { name: layout.block_type, alias: attribute_group } + oro_akeneo.product_variant.type_handler.string_type_handle: class: 'Oro\Bundle\AkeneoBundle\ProductVariant\TypeHandler\StringTypeHandler' arguments: