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