From 1c74f5576011cd442dc086e52935c9fe68e7aa3e Mon Sep 17 00:00:00 2001 From: eduard13 Date: Sun, 12 Aug 2018 22:40:08 +0300 Subject: [PATCH] Adding roles to images array --- .../Catalog/Model/ProductRepository.php | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/Catalog/Model/ProductRepository.php b/app/code/Magento/Catalog/Model/ProductRepository.php index 4c0122694285d..d7fbcbcd523f8 100644 --- a/app/code/Magento/Catalog/Model/ProductRepository.php +++ b/app/code/Magento/Catalog/Model/ProductRepository.php @@ -514,8 +514,13 @@ protected function processMediaGallery(ProductInterface $product, $mediaGalleryE $newEntries = $mediaGalleryEntries; } - $this->getMediaGalleryProcessor()->clearMediaAttribute($product, array_keys($product->getMediaAttributes())); $images = $product->getMediaGallery('images'); + if ($images) { + $images = $this->determineImageRoles($product, $images); + } + + $this->getMediaGalleryProcessor()->clearMediaAttribute($product, array_keys($product->getMediaAttributes())); + if ($images) { foreach ($images as $image) { if (!isset($image['removed']) && !empty($image['types'])) { @@ -758,6 +763,32 @@ public function cleanCache() $this->instancesById = null; } + /** + * 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; + } + /** * @return Product\Gallery\Processor */