Skip to content

Commit

Permalink
Fix magento#10687 - add types to images array
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Scarraban committed May 31, 2018
1 parent 9d6930c commit a99e97e
Showing 1 changed file with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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'])) {
Expand All @@ -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.
*
Expand Down

0 comments on commit a99e97e

Please sign in to comment.