Skip to content

Commit 1c74f55

Browse files
committed
Adding roles to images array
1 parent 6b7a9b3 commit 1c74f55

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

app/code/Magento/Catalog/Model/ProductRepository.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,8 +514,13 @@ protected function processMediaGallery(ProductInterface $product, $mediaGalleryE
514514
$newEntries = $mediaGalleryEntries;
515515
}
516516

517-
$this->getMediaGalleryProcessor()->clearMediaAttribute($product, array_keys($product->getMediaAttributes()));
518517
$images = $product->getMediaGallery('images');
518+
if ($images) {
519+
$images = $this->determineImageRoles($product, $images);
520+
}
521+
522+
$this->getMediaGalleryProcessor()->clearMediaAttribute($product, array_keys($product->getMediaAttributes()));
523+
519524
if ($images) {
520525
foreach ($images as $image) {
521526
if (!isset($image['removed']) && !empty($image['types'])) {
@@ -758,6 +763,32 @@ public function cleanCache()
758763
$this->instancesById = null;
759764
}
760765

766+
/**
767+
* Ascertain image roles, if they are not set against the gallery entries
768+
*
769+
* @param ProductInterface $product
770+
* @param array $images
771+
* @return array
772+
*/
773+
private function determineImageRoles(ProductInterface $product, array $images)
774+
{
775+
$imagesWithRoles = [];
776+
foreach ($images as $image) {
777+
if (!isset($image['types'])) {
778+
$image['types'] = [];
779+
if (isset($image['file'])) {
780+
foreach (array_keys($product->getMediaAttributes()) as $attribute) {
781+
if ($image['file'] == $product->getData($attribute)) {
782+
$image['types'][] = $attribute;
783+
}
784+
}
785+
}
786+
}
787+
$imagesWithRoles[] = $image;
788+
}
789+
return $imagesWithRoles;
790+
}
791+
761792
/**
762793
* @return Product\Gallery\Processor
763794
*/

0 commit comments

Comments
 (0)