/** * {@inheritdoc} */ public function save(CustomAttributesDataInterface $dataObjectWithCustomAttributes, $entityType, CustomAttributesDataInterface $previousCustomerData = null) { //Get all Image related custom attributes $imageDataObjects = $this->dataObjectHelper->getCustomAttributeValueByType($dataObjectWithCustomAttributes->getCustomAttributes(), '\\Magento\\Framework\\Api\\Data\\ImageContentInterface'); // Return if no images to process if (empty($imageDataObjects)) { return $dataObjectWithCustomAttributes; } // For every image, save it and replace it with corresponding Eav data object /** @var $imageDataObject \Magento\Framework\Api\AttributeValue */ foreach ($imageDataObjects as $imageDataObject) { /** @var $imageContent \Magento\Framework\Api\Data\ImageContentInterface */ $imageContent = $imageDataObject->getValue(); $filename = $this->processImageContent($entityType, $imageContent); //Set filename from static media location into data object $dataObjectWithCustomAttributes->setCustomAttribute($imageDataObject->getAttributeCode(), $filename); //Delete previously saved image if it exists if ($previousCustomerData) { $previousImageAttribute = $previousCustomerData->getCustomAttribute($imageDataObject->getAttributeCode()); if ($previousImageAttribute) { $previousImagePath = $previousImageAttribute->getValue(); if (!empty($previousImagePath)) { @unlink($this->mediaDirectory->getAbsolutePath() . $entityType . $previousImagePath); } } } } return $dataObjectWithCustomAttributes; }