Skip to content

Commit a99e97e

Browse files
committed
Fix magento#10687 - add types to images array
To fix magento#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
1 parent 9d6930c commit a99e97e

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,13 @@ public function processMediaGallery(ProductInterface $product, array $mediaGalle
100100
$newEntries = $mediaGalleryEntries;
101101
}
102102

103-
$this->processor->clearMediaAttribute($product, array_keys($product->getMediaAttributes()));
104103
$images = $product->getMediaGallery('images');
104+
105+
if ($images) {
106+
$images = $this->determineImageRoles($product, $images);
107+
}
108+
109+
$this->processor->clearMediaAttribute($product, array_keys($product->getMediaAttributes()));
105110
if ($images) {
106111
foreach ($images as $image) {
107112
if (!isset($image['removed']) && !empty($image['types'])) {
@@ -112,6 +117,32 @@ public function processMediaGallery(ProductInterface $product, array $mediaGalle
112117
$this->processEntries($product, $newEntries, $entriesById);
113118
}
114119

120+
/**
121+
* Ascertain image roles, if they are not set against the gallery entries
122+
*
123+
* @param ProductInterface $product
124+
* @param array $images
125+
* @return array
126+
*/
127+
private function determineImageRoles(ProductInterface $product, array $images)
128+
{
129+
$imagesWithRoles = [];
130+
foreach ($images as $image) {
131+
if (!isset($image['types'])) {
132+
$image['types'] = [];
133+
if (isset($image['file'])) {
134+
foreach (array_keys($product->getMediaAttributes()) as $attribute) {
135+
if ($image['file'] == $product->getData($attribute)) {
136+
$image['types'][] = $attribute;
137+
}
138+
}
139+
}
140+
}
141+
$imagesWithRoles[] = $image;
142+
}
143+
return $imagesWithRoles;
144+
}
145+
115146
/**
116147
* Convert entries into product media gallery data and set to product.
117148
*

0 commit comments

Comments
 (0)