Skip to content

Commit cfd6e98

Browse files
🔃 [EngCom] Public Pull Requests - 2.1-develop
Accepted Public Pull Requests: - #17553: [Backport] Fix #10687 - Product image roles disappearing (by @eduard13) Fixed GitHub Issues: - #10687: Product image roles randomly disappear (reported by @boxyman) has been fixed in #17553 by @eduard13 in 2.1-develop branch Related commits: 1. abb3c4a 2. 2be962d 3. 6a8fadf
2 parents 3e1caf5 + 7956e9b commit cfd6e98

File tree

1 file changed

+89
-31
lines changed

1 file changed

+89
-31
lines changed

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

Lines changed: 89 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -455,25 +455,19 @@ protected function processMediaGallery(ProductInterface $product, $mediaGalleryE
455455
$newEntries[] = $entry;
456456
}
457457
}
458-
foreach ($existingMediaGallery as $key => &$existingEntry) {
459-
if (isset($entriesById[$existingEntry['value_id']])) {
460-
$updatedEntry = $entriesById[$existingEntry['value_id']];
461-
if ($updatedEntry['file'] === null) {
462-
unset($updatedEntry['file']);
463-
}
464-
$existingMediaGallery[$key] = array_merge($existingEntry, $updatedEntry);
465-
} else {
466-
//set the removed flag
467-
$existingEntry['removed'] = true;
468-
}
469-
}
458+
$existingMediaGallery = $this->processingExistingImages($existingMediaGallery, $entriesById);
470459
$product->setData('media_gallery', ["images" => $existingMediaGallery]);
471460
} else {
472461
$newEntries = $mediaGalleryEntries;
473462
}
474463

475-
$this->getMediaGalleryProcessor()->clearMediaAttribute($product, array_keys($product->getMediaAttributes()));
476464
$images = $product->getMediaGallery('images');
465+
if ($images) {
466+
$images = $this->determineImageRoles($product, $images);
467+
}
468+
469+
$this->getMediaGalleryProcessor()->clearMediaAttribute($product, array_keys($product->getMediaAttributes()));
470+
477471
if ($images) {
478472
foreach ($images as $image) {
479473
if (!isset($image['removed']) && !empty($image['types'])) {
@@ -482,25 +476,8 @@ protected function processMediaGallery(ProductInterface $product, $mediaGalleryE
482476
}
483477
}
484478

485-
foreach ($newEntries as $newEntry) {
486-
if (!isset($newEntry['content'])) {
487-
throw new InputException(__('The image content is not valid.'));
488-
}
489-
/** @var ImageContentInterface $contentDataObject */
490-
$contentDataObject = $this->contentFactory->create()
491-
->setName($newEntry['content']['data'][ImageContentInterface::NAME])
492-
->setBase64EncodedData($newEntry['content']['data'][ImageContentInterface::BASE64_ENCODED_DATA])
493-
->setType($newEntry['content']['data'][ImageContentInterface::TYPE]);
494-
$newEntry['content'] = $contentDataObject;
495-
$this->processNewMediaGalleryEntry($product, $newEntry);
479+
$this->processingNewEntries($newEntries, $product, $entriesById);
496480

497-
$finalGallery = $product->getData('media_gallery');
498-
$newEntryId = key(array_diff_key($product->getData('media_gallery')['images'], $entriesById));
499-
$newEntry = array_replace_recursive($newEntry, $finalGallery['images'][$newEntryId]);
500-
$entriesById[$newEntryId] = $newEntry;
501-
$finalGallery['images'][$newEntryId] = $newEntry;
502-
$product->setData('media_gallery', $finalGallery);
503-
}
504481
return $this;
505482
}
506483

@@ -674,6 +651,32 @@ protected function addFilterGroupToCollection(
674651
}
675652
}
676653

654+
/**
655+
* Ascertain image roles, if they are not set against the gallery entries
656+
*
657+
* @param ProductInterface $product
658+
* @param array $images
659+
* @return array
660+
*/
661+
private function determineImageRoles(ProductInterface $product, array $images)
662+
{
663+
$imagesWithRoles = [];
664+
foreach ($images as $image) {
665+
if (!isset($image['types'])) {
666+
$image['types'] = [];
667+
if (isset($image['file'])) {
668+
foreach (array_keys($product->getMediaAttributes()) as $attribute) {
669+
if ($image['file'] == $product->getData($attribute)) {
670+
$image['types'][] = $attribute;
671+
}
672+
}
673+
}
674+
}
675+
$imagesWithRoles[] = $image;
676+
}
677+
return $imagesWithRoles;
678+
}
679+
677680
/**
678681
* Apply custom filters to product collection.
679682
*
@@ -729,4 +732,59 @@ private function getMediaGalleryProcessor()
729732
}
730733
return $this->mediaGalleryProcessor;
731734
}
735+
736+
/**
737+
* @param array $existingMediaGallery
738+
* @param array $entriesById
739+
* @return array
740+
*/
741+
private function processingExistingImages(array $existingMediaGallery, array $entriesById)
742+
{
743+
foreach ($existingMediaGallery as $key => &$existingEntry) {
744+
if (isset($entriesById[$existingEntry['value_id']])) {
745+
$updatedEntry = $entriesById[$existingEntry['value_id']];
746+
if ($updatedEntry['file'] === null) {
747+
unset($updatedEntry['file']);
748+
}
749+
$existingMediaGallery[$key] = array_merge($existingEntry, $updatedEntry);
750+
} else {
751+
//set the removed flag
752+
$existingEntry['removed'] = true;
753+
}
754+
}
755+
756+
return $existingMediaGallery;
757+
}
758+
759+
/**
760+
* @param array $newEntries
761+
* @param ProductInterface $product
762+
* @param array $entriesById
763+
* @return void
764+
* @throws InputException
765+
* @throws LocalizedException
766+
* @throws StateException
767+
*/
768+
private function processingNewEntries(array $newEntries, ProductInterface $product, array $entriesById)
769+
{
770+
foreach ($newEntries as $newEntry) {
771+
if (!isset($newEntry['content'])) {
772+
throw new InputException(__('The image content is not valid.'));
773+
}
774+
/** @var ImageContentInterface $contentDataObject */
775+
$contentDataObject = $this->contentFactory->create()
776+
->setName($newEntry['content']['data'][ImageContentInterface::NAME])
777+
->setBase64EncodedData($newEntry['content']['data'][ImageContentInterface::BASE64_ENCODED_DATA])
778+
->setType($newEntry['content']['data'][ImageContentInterface::TYPE]);
779+
$newEntry['content'] = $contentDataObject;
780+
$this->processNewMediaGalleryEntry($product, $newEntry);
781+
782+
$finalGallery = $product->getData('media_gallery');
783+
$newEntryId = key(array_diff_key($product->getData('media_gallery')['images'], $entriesById));
784+
$newEntry = array_replace_recursive($newEntry, $finalGallery['images'][$newEntryId]);
785+
$entriesById[$newEntryId] = $newEntry;
786+
$finalGallery['images'][$newEntryId] = $newEntry;
787+
$product->setData('media_gallery', $finalGallery);
788+
}
789+
}
732790
}

0 commit comments

Comments
 (0)