Skip to content

Commit

Permalink
Extract image and lavel processing for media attributes into their ow…
Browse files Browse the repository at this point in the history
…n method
  • Loading branch information
dmanners committed Nov 29, 2017
1 parent 1511377 commit bab1a38
Showing 1 changed file with 71 additions and 39 deletions.
110 changes: 71 additions & 39 deletions app/code/Magento/Catalog/Model/Product/Gallery/CreateHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,45 +167,13 @@ public function execute($product, $arguments = [])
if (empty($attrData) && empty($clearImages) && empty($newImages) && empty($existImages)) {
continue;
}
$resetLabel = false;
if (in_array($attrData, $clearImages)) {
$product->setData($mediaAttrCode, 'no_selection');
$product->setData($mediaAttrCode . '_label', null);
$resetLabel = true;
}

if (in_array($attrData, array_keys($newImages))) {
$product->setData($mediaAttrCode, $newImages[$attrData]['new_file']);
$product->setData($mediaAttrCode . '_label', $newImages[$attrData]['label']);
}

if (in_array($attrData, array_keys($existImages)) && isset($existImages[$attrData]['label'])) {
$product->setData($mediaAttrCode . '_label', $existImages[$attrData]['label']);
}

if ($attrData === 'no_selection' && !empty($product->getData($mediaAttrCode . '_label'))) {
$product->setData($mediaAttrCode . '_label', null);
$resetLabel = true;
}
if (!empty($product->getData($mediaAttrCode))) {
$product->addAttributeUpdate(
$mediaAttrCode,
$product->getData($mediaAttrCode),
$product->getStoreId()
);
}
if (in_array($mediaAttrCode, ['image', 'small_image', 'thumbnail']) &&
(
!empty($product->getData($mediaAttrCode . '_label'))
|| $resetLabel === true
)
) {
$product->addAttributeUpdate(
$mediaAttrCode . '_label',
$product->getData($mediaAttrCode . '_label'),
$product->getStoreId()
);
}
$this->processMediaAttribute(
$product,
$mediaAttrCode,
$clearImages,
$newImages,
$existImages
);
}

$product->setData($attrCode, $value);
Expand Down Expand Up @@ -468,4 +436,68 @@ private function getMediaAttributeCodes()
}
return $this->mediaAttributeCodes;
}

/**
* @param \Magento\Catalog\Model\Product $product
* @param $attrData
* @param array $clearImages
* @param $mediaAttrCode
* @param array $newImages
* @param array $existImages
*/
/**
* @param \Magento\Catalog\Model\Product $product
* @param $mediaAttrCode
* @param array $clearImages
* @param array $newImages
* @param array $existImages
*/
private function processMediaAttribute(
\Magento\Catalog\Model\Product $product,
$mediaAttrCode,
array $clearImages,
array $newImages,
array $existImages
) {
$resetLabel = false;
$attrData = $product->getData($mediaAttrCode);
if (in_array($attrData, $clearImages)) {
$product->setData($mediaAttrCode, 'no_selection');
$product->setData($mediaAttrCode . '_label', null);
$resetLabel = true;
}

if (in_array($attrData, array_keys($newImages))) {
$product->setData($mediaAttrCode, $newImages[$attrData]['new_file']);
$product->setData($mediaAttrCode . '_label', $newImages[$attrData]['label']);
}

if (in_array($attrData, array_keys($existImages)) && isset($existImages[$attrData]['label'])) {
$product->setData($mediaAttrCode . '_label', $existImages[$attrData]['label']);
}

if ($attrData === 'no_selection' && !empty($product->getData($mediaAttrCode . '_label'))) {
$product->setData($mediaAttrCode . '_label', null);
$resetLabel = true;
}
if (in_array($mediaAttrCode, ['image', 'small_image', 'thumbnail']) &&
(
!empty($product->getData($mediaAttrCode . '_label'))
|| $resetLabel === true
)
) {
$product->addAttributeUpdate(
$mediaAttrCode . '_label',
$product->getData($mediaAttrCode . '_label'),
$product->getStoreId()
);
}
if (!empty($product->getData($mediaAttrCode))) {
$product->addAttributeUpdate(
$mediaAttrCode,
$product->getData($mediaAttrCode),
$product->getStoreId()
);
}
}
}

0 comments on commit bab1a38

Please sign in to comment.