Skip to content

Commit abb3c4a

Browse files
committed
Adding roles to images array
1 parent 227acb8 commit abb3c4a

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
@@ -472,8 +472,13 @@ protected function processMediaGallery(ProductInterface $product, $mediaGalleryE
472472
$newEntries = $mediaGalleryEntries;
473473
}
474474

475-
$this->getMediaGalleryProcessor()->clearMediaAttribute($product, array_keys($product->getMediaAttributes()));
476475
$images = $product->getMediaGallery('images');
476+
if ($images) {
477+
$images = $this->determineImageRoles($product, $images);
478+
}
479+
480+
$this->getMediaGalleryProcessor()->clearMediaAttribute($product, array_keys($product->getMediaAttributes()));
481+
477482
if ($images) {
478483
foreach ($images as $image) {
479484
if (!isset($image['removed']) && !empty($image['types'])) {
@@ -674,6 +679,32 @@ protected function addFilterGroupToCollection(
674679
}
675680
}
676681

682+
/**
683+
* Ascertain image roles, if they are not set against the gallery entries
684+
*
685+
* @param ProductInterface $product
686+
* @param array $images
687+
* @return array
688+
*/
689+
private function determineImageRoles(ProductInterface $product, array $images)
690+
{
691+
$imagesWithRoles = [];
692+
foreach ($images as $image) {
693+
if (!isset($image['types'])) {
694+
$image['types'] = [];
695+
if (isset($image['file'])) {
696+
foreach (array_keys($product->getMediaAttributes()) as $attribute) {
697+
if ($image['file'] == $product->getData($attribute)) {
698+
$image['types'][] = $attribute;
699+
}
700+
}
701+
}
702+
}
703+
$imagesWithRoles[] = $image;
704+
}
705+
return $imagesWithRoles;
706+
}
707+
677708
/**
678709
* Apply custom filters to product collection.
679710
*

0 commit comments

Comments
 (0)