Skip to content

Commit bab1a38

Browse files
committed
Extract image and lavel processing for media attributes into their own method
1 parent 1511377 commit bab1a38

File tree

1 file changed

+71
-39
lines changed

1 file changed

+71
-39
lines changed

app/code/Magento/Catalog/Model/Product/Gallery/CreateHandler.php

Lines changed: 71 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -167,45 +167,13 @@ public function execute($product, $arguments = [])
167167
if (empty($attrData) && empty($clearImages) && empty($newImages) && empty($existImages)) {
168168
continue;
169169
}
170-
$resetLabel = false;
171-
if (in_array($attrData, $clearImages)) {
172-
$product->setData($mediaAttrCode, 'no_selection');
173-
$product->setData($mediaAttrCode . '_label', null);
174-
$resetLabel = true;
175-
}
176-
177-
if (in_array($attrData, array_keys($newImages))) {
178-
$product->setData($mediaAttrCode, $newImages[$attrData]['new_file']);
179-
$product->setData($mediaAttrCode . '_label', $newImages[$attrData]['label']);
180-
}
181-
182-
if (in_array($attrData, array_keys($existImages)) && isset($existImages[$attrData]['label'])) {
183-
$product->setData($mediaAttrCode . '_label', $existImages[$attrData]['label']);
184-
}
185-
186-
if ($attrData === 'no_selection' && !empty($product->getData($mediaAttrCode . '_label'))) {
187-
$product->setData($mediaAttrCode . '_label', null);
188-
$resetLabel = true;
189-
}
190-
if (!empty($product->getData($mediaAttrCode))) {
191-
$product->addAttributeUpdate(
192-
$mediaAttrCode,
193-
$product->getData($mediaAttrCode),
194-
$product->getStoreId()
195-
);
196-
}
197-
if (in_array($mediaAttrCode, ['image', 'small_image', 'thumbnail']) &&
198-
(
199-
!empty($product->getData($mediaAttrCode . '_label'))
200-
|| $resetLabel === true
201-
)
202-
) {
203-
$product->addAttributeUpdate(
204-
$mediaAttrCode . '_label',
205-
$product->getData($mediaAttrCode . '_label'),
206-
$product->getStoreId()
207-
);
208-
}
170+
$this->processMediaAttribute(
171+
$product,
172+
$mediaAttrCode,
173+
$clearImages,
174+
$newImages,
175+
$existImages
176+
);
209177
}
210178

211179
$product->setData($attrCode, $value);
@@ -468,4 +436,68 @@ private function getMediaAttributeCodes()
468436
}
469437
return $this->mediaAttributeCodes;
470438
}
439+
440+
/**
441+
* @param \Magento\Catalog\Model\Product $product
442+
* @param $attrData
443+
* @param array $clearImages
444+
* @param $mediaAttrCode
445+
* @param array $newImages
446+
* @param array $existImages
447+
*/
448+
/**
449+
* @param \Magento\Catalog\Model\Product $product
450+
* @param $mediaAttrCode
451+
* @param array $clearImages
452+
* @param array $newImages
453+
* @param array $existImages
454+
*/
455+
private function processMediaAttribute(
456+
\Magento\Catalog\Model\Product $product,
457+
$mediaAttrCode,
458+
array $clearImages,
459+
array $newImages,
460+
array $existImages
461+
) {
462+
$resetLabel = false;
463+
$attrData = $product->getData($mediaAttrCode);
464+
if (in_array($attrData, $clearImages)) {
465+
$product->setData($mediaAttrCode, 'no_selection');
466+
$product->setData($mediaAttrCode . '_label', null);
467+
$resetLabel = true;
468+
}
469+
470+
if (in_array($attrData, array_keys($newImages))) {
471+
$product->setData($mediaAttrCode, $newImages[$attrData]['new_file']);
472+
$product->setData($mediaAttrCode . '_label', $newImages[$attrData]['label']);
473+
}
474+
475+
if (in_array($attrData, array_keys($existImages)) && isset($existImages[$attrData]['label'])) {
476+
$product->setData($mediaAttrCode . '_label', $existImages[$attrData]['label']);
477+
}
478+
479+
if ($attrData === 'no_selection' && !empty($product->getData($mediaAttrCode . '_label'))) {
480+
$product->setData($mediaAttrCode . '_label', null);
481+
$resetLabel = true;
482+
}
483+
if (in_array($mediaAttrCode, ['image', 'small_image', 'thumbnail']) &&
484+
(
485+
!empty($product->getData($mediaAttrCode . '_label'))
486+
|| $resetLabel === true
487+
)
488+
) {
489+
$product->addAttributeUpdate(
490+
$mediaAttrCode . '_label',
491+
$product->getData($mediaAttrCode . '_label'),
492+
$product->getStoreId()
493+
);
494+
}
495+
if (!empty($product->getData($mediaAttrCode))) {
496+
$product->addAttributeUpdate(
497+
$mediaAttrCode,
498+
$product->getData($mediaAttrCode),
499+
$product->getStoreId()
500+
);
501+
}
502+
}
471503
}

0 commit comments

Comments
 (0)