attributeRepository = $attributeRepository; $this->metadataPool = $metadataPool; $this->searchCriteriaBuilder = $searchCriteriaBuilder; $this->attributePersistor = $attributePersistor; $this->scopeResolver = $scopeResolver; $this->attributeLoader = $attributeLoader ?: ObjectManager::getInstance()->get(AttributeLoader::class); } /** * @param string $entityType * @param int $attributeSetId * @return \Magento\Eav\Api\Data\AttributeInterface[] */ protected function getAttributes($entityType, $attributeSetId = null) { return $this->attributeLoader->getAttributes($entityType, $attributeSetId); } /** * @param string $entityType * @param array $entityData * @param array $arguments * @return array * @throws \Exception * @throws \Magento\Framework\Exception\ConfigurationMismatchException * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function execute($entityType, $entityData, $arguments = []) { $metadata = $this->metadataPool->getMetadata($entityType); if ($metadata->getEavEntityType()) { $processed = []; $entityLinkField = $metadata->getLinkField(); $attributeSetId = isset($entityData[AttributeLoader::ATTRIBUTE_SET_ID]) ? $entityData[AttributeLoader::ATTRIBUTE_SET_ID] : null; // @todo verify is it normal to not have attribute_set_id /** @var \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute */ foreach ($this->getAttributes($entityType, $attributeSetId) as $attribute) { if ($attribute->isStatic()) { continue; } $attributeCode = $attribute->getAttributeCode(); if (isset($entityData[$attributeCode]) && !is_array($entityData[$attributeCode]) && !$attribute->isValueEmpty($entityData[$attributeCode]) ) { $this->attributePersistor->registerInsert( $entityType, $entityData[$entityLinkField], $attributeCode, $entityData[$attributeCode] ); $processed[$attributeCode] = $entityData[$attributeCode]; } } $context = $this->scopeResolver->getEntityContext($entityType, $entityData); $this->attributePersistor->flush($entityType, $context); } return $entityData; } }