From a99e97e1ebbb3fd8b09e3a7236813831a76c7cef Mon Sep 17 00:00:00 2001 From: Sam Butler-Thompson Date: Thu, 31 May 2018 06:40:01 +0100 Subject: [PATCH] Fix #10687 - add types to images array To fix #10687 - adds the 'types' key to the $images array to provide existing image roles (if not set), which are re-attributed after clearMediaAttribute() is called --- .../MediaGalleryProcessor.php | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) 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. *