@@ -493,6 +493,7 @@ private function processLinks(\Magento\Catalog\Api\Data\ProductInterface $produc
493493 * @return $this
494494 * @throws InputException
495495 * @throws StateException
496+ * @throws LocalizedException
496497 * @SuppressWarnings(PHPMD.CyclomaticComplexity)
497498 */
498499 protected function processMediaGallery (ProductInterface $ product , $ mediaGalleryEntries )
@@ -502,16 +503,16 @@ protected function processMediaGallery(ProductInterface $product, $mediaGalleryE
502503 $ entriesById = [];
503504 if (!empty ($ existingMediaGallery )) {
504505 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 ;
507508 } else {
508509 $ newEntries [] = $ entry ;
509510 }
510511 }
511512 foreach ($ existingMediaGallery as $ key => &$ existingEntry ) {
512513 if (isset ($ entriesById [$ existingEntry ['value_id ' ]])) {
513514 $ updatedEntry = $ entriesById [$ existingEntry ['value_id ' ]];
514- if ($ updatedEntry ['file ' ] === null ) {
515+ if (array_key_exists ( ' file ' , $ updatedEntry ) && $ updatedEntry ['file ' ] === null ) {
515516 unset($ updatedEntry ['file ' ]);
516517 }
517518 $ existingMediaGallery [$ key ] = array_merge ($ existingEntry , $ updatedEntry );
@@ -520,6 +521,7 @@ protected function processMediaGallery(ProductInterface $product, $mediaGalleryE
520521 $ existingEntry ['removed ' ] = true ;
521522 }
522523 }
524+ unset($ existingEntry );
523525 $ product ->setData ('media_gallery ' , ["images " => $ existingMediaGallery ]);
524526 } else {
525527 $ newEntries = $ mediaGalleryEntries ;
@@ -534,26 +536,8 @@ protected function processMediaGallery(ProductInterface $product, $mediaGalleryE
534536 }
535537 }
536538 }
539+ $ this ->processEntries ($ product , $ newEntries , $ entriesById );
537540
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- }
557541 return $ this ;
558542 }
559543
@@ -592,8 +576,8 @@ public function save(\Magento\Catalog\Api\Data\ProductInterface $product, $saveO
592576 $ product = $ this ->initializeProductData ($ productDataArray , empty ($ existingProduct ));
593577
594578 $ 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 ' ]);
597581 }
598582
599583 if (!$ product ->getOptionsReadonly ()) {
@@ -791,4 +775,60 @@ private function getCollectionProcessor()
791775 }
792776 return $ this ->collectionProcessor ;
793777 }
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+ }
794834}
0 commit comments