diff --git a/app/code/Magento/Catalog/Model/ProductRepository/MediaGalleryProcessor.php b/app/code/Magento/Catalog/Model/ProductRepository/MediaGalleryProcessor.php index 4cc31d98fdfc2..56d1cfda8bce5 100644 --- a/app/code/Magento/Catalog/Model/ProductRepository/MediaGalleryProcessor.php +++ b/app/code/Magento/Catalog/Model/ProductRepository/MediaGalleryProcessor.php @@ -100,8 +100,13 @@ public function processMediaGallery(ProductInterface $product, array $mediaGalle $newEntries = $mediaGalleryEntries; } - $this->processor->clearMediaAttribute($product, array_keys($product->getMediaAttributes())); $images = $product->getMediaGallery('images'); + + if ($images) { + $images = $this->determineImageRoles($product, $images); + } + + $this->processor->clearMediaAttribute($product, array_keys($product->getMediaAttributes())); if ($images) { foreach ($images as $image) { if (!isset($image['removed']) && !empty($image['types'])) { @@ -112,6 +117,32 @@ public function processMediaGallery(ProductInterface $product, array $mediaGalle $this->processEntries($product, $newEntries, $entriesById); } + /** + * Ascertain image roles, if they are not set against the gallery entries + * + * @param ProductInterface $product + * @param array $images + * @return array + */ + private function determineImageRoles(ProductInterface $product, array $images) + { + $imagesWithRoles = []; + foreach ($images as $image) { + if (!isset($image['types'])) { + $image['types'] = []; + if (isset($image['file'])) { + foreach (array_keys($product->getMediaAttributes()) as $attribute) { + if ($image['file'] == $product->getData($attribute)) { + $image['types'][] = $attribute; + } + } + } + } + $imagesWithRoles[] = $image; + } + return $imagesWithRoles; + } + /** * Convert entries into product media gallery data and set to product. *