Skip to content

[Forwardport] Fix #10687 - Product image roles disappearing #17554

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion app/code/Magento/Catalog/Model/ProductRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])) {
Expand Down Expand Up @@ -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
*/
Expand Down