Skip to content

Commit 1bb6d00

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-61135' into 2.1.8-develop-pr19
2 parents c5b99df + 448ef20 commit 1bb6d00

File tree

2 files changed

+72
-12
lines changed

2 files changed

+72
-12
lines changed

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

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ protected function getCacheKey($data)
278278
*/
279279
protected function initializeProductData(array $productData, $createNew)
280280
{
281+
unset($productData['media_gallery']);
281282
if ($createNew) {
282283
$product = $this->productFactory->create();
283284
if ($this->storeManager->hasSingleStore()) {
@@ -420,8 +421,15 @@ private function processLinks(\Magento\Catalog\Api\Data\ProductInterface $produc
420421
}
421422

422423
/**
423-
* @param ProductInterface $product
424-
* @param array $mediaGalleryEntries
424+
* Process Media gallery data before save product.
425+
*
426+
* Compare Media Gallery Entries Data with existing Media Gallery
427+
* * If Media entry has not value_id set it as new
428+
* * If Existing entry 'value_id' absent in Media Gallery set 'removed' flag
429+
* * Merge Existing and new media gallery
430+
*
431+
* @param ProductInterface $product contains only existing media gallery items
432+
* @param array $mediaGalleryEntries array which contains all media gallery items
425433
* @return $this
426434
* @throws InputException
427435
* @throws StateException
@@ -431,11 +439,10 @@ protected function processMediaGallery(ProductInterface $product, $mediaGalleryE
431439
{
432440
$existingMediaGallery = $product->getMediaGallery('images');
433441
$newEntries = [];
442+
$entriesById = [];
434443
if (!empty($existingMediaGallery)) {
435-
$entriesById = [];
436444
foreach ($mediaGalleryEntries as $entry) {
437-
if (isset($entry['id'])) {
438-
$entry['value_id'] = $entry['id'];
445+
if (isset($entry['value_id'])) {
439446
$entriesById[$entry['value_id']] = $entry;
440447
} else {
441448
$newEntries[] = $entry;
@@ -444,6 +451,9 @@ protected function processMediaGallery(ProductInterface $product, $mediaGalleryE
444451
foreach ($existingMediaGallery as $key => &$existingEntry) {
445452
if (isset($entriesById[$existingEntry['value_id']])) {
446453
$updatedEntry = $entriesById[$existingEntry['value_id']];
454+
if ($updatedEntry['file'] === null) {
455+
unset($updatedEntry['file']);
456+
}
447457
$existingMediaGallery[$key] = array_merge($existingEntry, $updatedEntry);
448458
} else {
449459
//set the removed flag
@@ -471,11 +481,18 @@ protected function processMediaGallery(ProductInterface $product, $mediaGalleryE
471481
}
472482
/** @var ImageContentInterface $contentDataObject */
473483
$contentDataObject = $this->contentFactory->create()
474-
->setName($newEntry['content'][ImageContentInterface::NAME])
475-
->setBase64EncodedData($newEntry['content'][ImageContentInterface::BASE64_ENCODED_DATA])
476-
->setType($newEntry['content'][ImageContentInterface::TYPE]);
484+
->setName($newEntry['content']['data'][ImageContentInterface::NAME])
485+
->setBase64EncodedData($newEntry['content']['data'][ImageContentInterface::BASE64_ENCODED_DATA])
486+
->setType($newEntry['content']['data'][ImageContentInterface::TYPE]);
477487
$newEntry['content'] = $contentDataObject;
478488
$this->processNewMediaGalleryEntry($product, $newEntry);
489+
490+
$finalGallery = $product->getData('media_gallery');
491+
$newEntryId = key(array_diff_key($product->getData('media_gallery')['images'], $entriesById));
492+
$newEntry = array_replace_recursive($newEntry, $finalGallery['images'][$newEntryId]);
493+
$entriesById[$newEntryId] = $newEntry;
494+
$finalGallery['images'][$newEntryId] = $newEntry;
495+
$product->setData('media_gallery', $finalGallery);
479496
}
480497
return $this;
481498
}
@@ -503,8 +520,6 @@ public function save(\Magento\Catalog\Api\Data\ProductInterface $product, $saveO
503520
$productDataArray = $this->extensibleDataObjectConverter
504521
->toNestedArray($product, [], 'Magento\Catalog\Api\Data\ProductInterface');
505522
$productDataArray = array_replace($productDataArray, $product->getData());
506-
unset($productDataArray['media_gallery']);
507-
508523
$ignoreLinksFlag = $product->getData('ignore_links_flag');
509524
$productLinks = null;
510525
if (!$ignoreLinksFlag && $ignoreLinksFlag !== null) {
@@ -514,8 +529,8 @@ public function save(\Magento\Catalog\Api\Data\ProductInterface $product, $saveO
514529
$product = $this->initializeProductData($productDataArray, empty($existingProduct));
515530

516531
$this->processLinks($product, $productLinks);
517-
if (isset($productDataArray['media_gallery_entries'])) {
518-
$this->processMediaGallery($product, $productDataArray['media_gallery_entries']);
532+
if (isset($productDataArray['media_gallery'])) {
533+
$this->processMediaGallery($product, $productDataArray['media_gallery']['images']);
519534
}
520535

521536
if (!$product->getOptionsReadonly()) {

dev/tests/api-functional/testsuite/Magento/Catalog/Api/ProductAttributeMediaGalleryManagementInterfaceTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,4 +615,49 @@ public function testGetListForAbsentSku()
615615
}
616616
$this->_webApiCall($serviceInfo, $requestData);
617617
}
618+
619+
/**
620+
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
621+
*/
622+
public function testAddProductVideo()
623+
{
624+
$videoContent = [
625+
'media_type' => 'external-video',
626+
'video_provider' => 'vimeo',
627+
'video_url' => 'https://vimeo.com/testUrl',
628+
'video_title' => 'Vimeo Test Title',
629+
'video_description' => 'test description',
630+
'video_metadata' => 'video meta data'
631+
];
632+
633+
$requestData = [
634+
'id' => null,
635+
'media_type' => 'external-video',
636+
'label' => 'Image Text',
637+
'position' => 1,
638+
'types' => null,
639+
'disabled' => false,
640+
'content' => [
641+
ImageContentInterface::BASE64_ENCODED_DATA => base64_encode(file_get_contents($this->testImagePath)),
642+
ImageContentInterface::TYPE => 'image/jpeg',
643+
ImageContentInterface::NAME => 'test_image.jpg'
644+
],
645+
'extension_attributes' => [
646+
'video_content' => $videoContent
647+
]
648+
];
649+
650+
$actualResult = $this->_webApiCall($this->createServiceInfo, ['sku' => 'simple', 'entry' => $requestData]);
651+
$targetProduct = $this->getTargetSimpleProduct();
652+
$mediaGallery = $targetProduct->getData('media_gallery');
653+
654+
$this->assertCount(1, $mediaGallery['images']);
655+
$updatedImage = array_shift($mediaGallery['images']);
656+
$this->assertEquals($actualResult, $updatedImage['value_id']);
657+
$this->assertEquals('Image Text', $updatedImage['label']);
658+
$this->assertEquals(1, $updatedImage['position']);
659+
$this->assertEquals(0, $updatedImage['disabled']);
660+
$this->assertStringStartsWith('/t/e/test_image', $updatedImage['file']);
661+
$this->assertEquals($videoContent, array_intersect($updatedImage, $videoContent));
662+
}
618663
}

0 commit comments

Comments
 (0)