@@ -488,6 +488,7 @@ private function processLinks(\Magento\Catalog\Api\Data\ProductInterface $produc
488
488
* @return $this
489
489
* @throws InputException
490
490
* @throws StateException
491
+ * @throws LocalizedException
491
492
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
492
493
*/
493
494
protected function processMediaGallery (ProductInterface $ product , $ mediaGalleryEntries )
@@ -497,16 +498,16 @@ protected function processMediaGallery(ProductInterface $product, $mediaGalleryE
497
498
$ entriesById = [];
498
499
if (!empty ($ existingMediaGallery )) {
499
500
foreach ($ mediaGalleryEntries as $ entry ) {
500
- if (isset ($ entry ['value_id ' ])) {
501
- $ entriesById [$ entry ['value_id ' ]] = $ entry ;
501
+ if (isset ($ entry ['id ' ])) {
502
+ $ entriesById [$ entry ['id ' ]] = $ entry ;
502
503
} else {
503
504
$ newEntries [] = $ entry ;
504
505
}
505
506
}
506
507
foreach ($ existingMediaGallery as $ key => &$ existingEntry ) {
507
508
if (isset ($ entriesById [$ existingEntry ['value_id ' ]])) {
508
509
$ updatedEntry = $ entriesById [$ existingEntry ['value_id ' ]];
509
- if ($ updatedEntry ['file ' ] === null ) {
510
+ if (array_key_exists ( ' file ' , $ updatedEntry ) && $ updatedEntry ['file ' ] === null ) {
510
511
unset($ updatedEntry ['file ' ]);
511
512
}
512
513
$ existingMediaGallery [$ key ] = array_merge ($ existingEntry , $ updatedEntry );
@@ -515,6 +516,7 @@ protected function processMediaGallery(ProductInterface $product, $mediaGalleryE
515
516
$ existingEntry ['removed ' ] = true ;
516
517
}
517
518
}
519
+ unset($ existingEntry );
518
520
$ product ->setData ('media_gallery ' , ["images " => $ existingMediaGallery ]);
519
521
} else {
520
522
$ newEntries = $ mediaGalleryEntries ;
@@ -529,26 +531,8 @@ protected function processMediaGallery(ProductInterface $product, $mediaGalleryE
529
531
}
530
532
}
531
533
}
534
+ $ this ->processEntries ($ product , $ newEntries , $ entriesById );
532
535
533
- foreach ($ newEntries as $ newEntry ) {
534
- if (!isset ($ newEntry ['content ' ])) {
535
- throw new InputException (__ ('The image content is not valid. ' ));
536
- }
537
- /** @var ImageContentInterface $contentDataObject */
538
- $ contentDataObject = $ this ->contentFactory ->create ()
539
- ->setName ($ newEntry ['content ' ]['data ' ][ImageContentInterface::NAME ])
540
- ->setBase64EncodedData ($ newEntry ['content ' ]['data ' ][ImageContentInterface::BASE64_ENCODED_DATA ])
541
- ->setType ($ newEntry ['content ' ]['data ' ][ImageContentInterface::TYPE ]);
542
- $ newEntry ['content ' ] = $ contentDataObject ;
543
- $ this ->processNewMediaGalleryEntry ($ product , $ newEntry );
544
-
545
- $ finalGallery = $ product ->getData ('media_gallery ' );
546
- $ newEntryId = key (array_diff_key ($ product ->getData ('media_gallery ' )['images ' ], $ entriesById ));
547
- $ newEntry = array_replace_recursive ($ newEntry , $ finalGallery ['images ' ][$ newEntryId ]);
548
- $ entriesById [$ newEntryId ] = $ newEntry ;
549
- $ finalGallery ['images ' ][$ newEntryId ] = $ newEntry ;
550
- $ product ->setData ('media_gallery ' , $ finalGallery );
551
- }
552
536
return $ this ;
553
537
}
554
538
@@ -587,8 +571,8 @@ public function save(\Magento\Catalog\Api\Data\ProductInterface $product, $saveO
587
571
$ product = $ this ->initializeProductData ($ productDataArray , empty ($ existingProduct ));
588
572
589
573
$ this ->processLinks ($ product , $ productLinks );
590
- if (isset ($ productDataArray ['media_gallery ' ])) {
591
- $ this ->processMediaGallery ($ product , $ productDataArray ['media_gallery ' ][ ' images ' ]);
574
+ if (isset ($ productDataArray ['media_gallery_entries ' ])) {
575
+ $ this ->processMediaGallery ($ product , $ productDataArray ['media_gallery_entries ' ]);
592
576
}
593
577
594
578
if (!$ product ->getOptionsReadonly ()) {
@@ -786,4 +770,60 @@ private function getCollectionProcessor()
786
770
}
787
771
return $ this ->collectionProcessor ;
788
772
}
773
+
774
+ /**
775
+ * Convert extension attribute for product media gallery.
776
+ *
777
+ * @param array $newEntry
778
+ * @param array $extensionAttributes
779
+ * @return void
780
+ */
781
+ private function processExtensionAttributes (array &$ newEntry , array $ extensionAttributes )
782
+ {
783
+ foreach ($ extensionAttributes as $ code => $ value ) {
784
+ if (is_array ($ value )) {
785
+ $ this ->processExtensionAttributes ($ newEntry , $ value );
786
+ } else {
787
+ $ newEntry [$ code ] = $ value ;
788
+ }
789
+ }
790
+ unset($ newEntry ['extension_attributes ' ]);
791
+ }
792
+
793
+ /**
794
+ * Convert entries into product media gallery data and set to product.
795
+ *
796
+ * @param ProductInterface $product
797
+ * @param array $newEntries
798
+ * @param array $entriesById
799
+ * @throws InputException
800
+ * @throws LocalizedException
801
+ * @throws StateException
802
+ * @return void
803
+ */
804
+ private function processEntries (ProductInterface $ product , array $ newEntries , array $ entriesById )
805
+ {
806
+ foreach ($ newEntries as $ newEntry ) {
807
+ if (!isset ($ newEntry ['content ' ])) {
808
+ throw new InputException (__ ('The image content is not valid. ' ));
809
+ }
810
+ /** @var ImageContentInterface $contentDataObject */
811
+ $ contentDataObject = $ this ->contentFactory ->create ()
812
+ ->setName ($ newEntry ['content ' ][ImageContentInterface::NAME ])
813
+ ->setBase64EncodedData ($ newEntry ['content ' ][ImageContentInterface::BASE64_ENCODED_DATA ])
814
+ ->setType ($ newEntry ['content ' ][ImageContentInterface::TYPE ]);
815
+ $ newEntry ['content ' ] = $ contentDataObject ;
816
+ $ this ->processNewMediaGalleryEntry ($ product , $ newEntry );
817
+
818
+ $ finalGallery = $ product ->getData ('media_gallery ' );
819
+ $ newEntryId = key (array_diff_key ($ product ->getData ('media_gallery ' )['images ' ], $ entriesById ));
820
+ if (isset ($ newEntry ['extension_attributes ' ])) {
821
+ $ this ->processExtensionAttributes ($ newEntry , $ newEntry ['extension_attributes ' ]);
822
+ }
823
+ $ newEntry = array_replace_recursive ($ newEntry , $ finalGallery ['images ' ][$ newEntryId ]);
824
+ $ entriesById [$ newEntryId ] = $ newEntry ;
825
+ $ finalGallery ['images ' ][$ newEntryId ] = $ newEntry ;
826
+ $ product ->setData ('media_gallery ' , $ finalGallery );
827
+ }
828
+ }
789
829
}
0 commit comments