/** * @param \Magento\Framework\Object $object * @return void * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function afterSave($object) { if ($object->getIsDuplicate() == true) { $this->duplicate($object); return; } $attrCode = $this->getAttribute()->getAttributeCode(); $value = $object->getData($attrCode); if (!is_array($value) || !isset($value['images']) || $object->isLockedAttribute($attrCode)) { return; } $storeId = $object->getStoreId(); $storeIds = $object->getStoreIds(); $storeIds[] = \Magento\Store\Model\Store::DEFAULT_STORE_ID; // remove current storeId $storeIds = array_flip($storeIds); unset($storeIds[$storeId]); $storeIds = array_keys($storeIds); $images = $this->_productFactory->create()->getAssignedImages($object, $storeIds); $picturesInOtherStores = []; foreach ($images as $image) { $picturesInOtherStores[$image['filepath']] = true; } $recordsToDelete = []; $filesToDelete = []; foreach ($value['images'] as &$image) { if (!empty($image['removed'])) { if (!empty($image['value_id']) && !isset($picturesInOtherStores[$image['file']])) { $recordsToDelete[] = $image['value_id']; $filesToDelete[] = ltrim($image['file'], '/'); } continue; } if (empty($image['value_id'])) { $data = []; $data['entity_id'] = $object->getId(); $data['attribute_id'] = $this->getAttribute()->getId(); $data['value'] = $image['file']; $image['value_id'] = $this->_getResource()->insertGallery($data); } $this->_getResource()->deleteGalleryValueInStore($image['value_id'], $object->getStoreId()); // Add per store labels, position, disabled $data = []; $data['value_id'] = $image['value_id']; $data['label'] = isset($image['label']) ? $image['label'] : ''; $data['position'] = isset($image['position']) ? (int) $image['position'] : 0; $data['disabled'] = isset($image['disabled']) ? (int) $image['disabled'] : 0; $data['store_id'] = (int) $object->getStoreId(); $data['entity_id'] = (int) $object->getId(); $this->_getResource()->insertGalleryValueInStore($data); } $this->_getResource()->deleteGallery($recordsToDelete); $this->removeDeletedImages($filesToDelete); $object->setData($attrCode, $value); }
/** * Initialize attribute sets code-to-id pairs. * * @return $this */ protected function _initAttributeSets() { $productTypeId = $this->_productFactory->create()->getTypeId(); foreach ($this->_attrSetColFactory->create()->setEntityTypeFilter($productTypeId) as $attributeSet) { $this->_attrSetIdToName[$attributeSet->getId()] = $attributeSet->getAttributeSetName(); } return $this; }