|
| 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 | +} |
0 commit comments