Skip to content

Commit d4452fc

Browse files
author
Sergey Shvets
committed
Merge branch '2.1.8-develop' of github.com:magento-east/magento2ce into MAGETWO-68811
2 parents 3b89704 + 0ad9891 commit d4452fc

File tree

67 files changed

+3110
-443
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+3110
-443
lines changed

CONTRIBUTING.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,8 @@ If you are a new GitHub user, we recommend that you create your own [free github
2929
3. Create and test your work.
3030
4. Fork the Magento 2 repository according to [Fork a repository instructions](http://devdocs.magento.com/guides/v2.0/contributor-guide/contributing.html#fork) and when you are ready to send us a pull request – follow [Create a pull request instructions](http://devdocs.magento.com/guides/v2.0/contributor-guide/contributing.html#pull_request).
3131
5. Once your contribution is received, Magento 2 development team will review the contribution and collaborate with you as needed to improve the quality of the contribution.
32+
33+
## Code of Conduct
34+
35+
Please note that this project is released with a Contributor Code of Conduct. We expect you to agree to its terms when participating in this project.
36+
The full text is available in the repository [Wiki](https://github.com/magento/magento2/wiki/Magento-Code-of-Conduct).

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()) {

0 commit comments

Comments
 (0)