@@ -455,25 +455,19 @@ protected function processMediaGallery(ProductInterface $product, $mediaGalleryE
455
455
$ newEntries [] = $ entry ;
456
456
}
457
457
}
458
- foreach ($ existingMediaGallery as $ key => &$ existingEntry ) {
459
- if (isset ($ entriesById [$ existingEntry ['value_id ' ]])) {
460
- $ updatedEntry = $ entriesById [$ existingEntry ['value_id ' ]];
461
- if ($ updatedEntry ['file ' ] === null ) {
462
- unset($ updatedEntry ['file ' ]);
463
- }
464
- $ existingMediaGallery [$ key ] = array_merge ($ existingEntry , $ updatedEntry );
465
- } else {
466
- //set the removed flag
467
- $ existingEntry ['removed ' ] = true ;
468
- }
469
- }
458
+ $ existingMediaGallery = $ this ->processingExistingImages ($ existingMediaGallery , $ entriesById );
470
459
$ product ->setData ('media_gallery ' , ["images " => $ existingMediaGallery ]);
471
460
} else {
472
461
$ newEntries = $ mediaGalleryEntries ;
473
462
}
474
463
475
- $ this ->getMediaGalleryProcessor ()->clearMediaAttribute ($ product , array_keys ($ product ->getMediaAttributes ()));
476
464
$ images = $ product ->getMediaGallery ('images ' );
465
+ if ($ images ) {
466
+ $ images = $ this ->determineImageRoles ($ product , $ images );
467
+ }
468
+
469
+ $ this ->getMediaGalleryProcessor ()->clearMediaAttribute ($ product , array_keys ($ product ->getMediaAttributes ()));
470
+
477
471
if ($ images ) {
478
472
foreach ($ images as $ image ) {
479
473
if (!isset ($ image ['removed ' ]) && !empty ($ image ['types ' ])) {
@@ -482,25 +476,8 @@ protected function processMediaGallery(ProductInterface $product, $mediaGalleryE
482
476
}
483
477
}
484
478
485
- foreach ($ newEntries as $ newEntry ) {
486
- if (!isset ($ newEntry ['content ' ])) {
487
- throw new InputException (__ ('The image content is not valid. ' ));
488
- }
489
- /** @var ImageContentInterface $contentDataObject */
490
- $ contentDataObject = $ this ->contentFactory ->create ()
491
- ->setName ($ newEntry ['content ' ]['data ' ][ImageContentInterface::NAME ])
492
- ->setBase64EncodedData ($ newEntry ['content ' ]['data ' ][ImageContentInterface::BASE64_ENCODED_DATA ])
493
- ->setType ($ newEntry ['content ' ]['data ' ][ImageContentInterface::TYPE ]);
494
- $ newEntry ['content ' ] = $ contentDataObject ;
495
- $ this ->processNewMediaGalleryEntry ($ product , $ newEntry );
479
+ $ this ->processingNewEntries ($ newEntries , $ product , $ entriesById );
496
480
497
- $ finalGallery = $ product ->getData ('media_gallery ' );
498
- $ newEntryId = key (array_diff_key ($ product ->getData ('media_gallery ' )['images ' ], $ entriesById ));
499
- $ newEntry = array_replace_recursive ($ newEntry , $ finalGallery ['images ' ][$ newEntryId ]);
500
- $ entriesById [$ newEntryId ] = $ newEntry ;
501
- $ finalGallery ['images ' ][$ newEntryId ] = $ newEntry ;
502
- $ product ->setData ('media_gallery ' , $ finalGallery );
503
- }
504
481
return $ this ;
505
482
}
506
483
@@ -674,6 +651,32 @@ protected function addFilterGroupToCollection(
674
651
}
675
652
}
676
653
654
+ /**
655
+ * Ascertain image roles, if they are not set against the gallery entries
656
+ *
657
+ * @param ProductInterface $product
658
+ * @param array $images
659
+ * @return array
660
+ */
661
+ private function determineImageRoles (ProductInterface $ product , array $ images )
662
+ {
663
+ $ imagesWithRoles = [];
664
+ foreach ($ images as $ image ) {
665
+ if (!isset ($ image ['types ' ])) {
666
+ $ image ['types ' ] = [];
667
+ if (isset ($ image ['file ' ])) {
668
+ foreach (array_keys ($ product ->getMediaAttributes ()) as $ attribute ) {
669
+ if ($ image ['file ' ] == $ product ->getData ($ attribute )) {
670
+ $ image ['types ' ][] = $ attribute ;
671
+ }
672
+ }
673
+ }
674
+ }
675
+ $ imagesWithRoles [] = $ image ;
676
+ }
677
+ return $ imagesWithRoles ;
678
+ }
679
+
677
680
/**
678
681
* Apply custom filters to product collection.
679
682
*
@@ -729,4 +732,59 @@ private function getMediaGalleryProcessor()
729
732
}
730
733
return $ this ->mediaGalleryProcessor ;
731
734
}
735
+
736
+ /**
737
+ * @param array $existingMediaGallery
738
+ * @param array $entriesById
739
+ * @return array
740
+ */
741
+ private function processingExistingImages (array $ existingMediaGallery , array $ entriesById )
742
+ {
743
+ foreach ($ existingMediaGallery as $ key => &$ existingEntry ) {
744
+ if (isset ($ entriesById [$ existingEntry ['value_id ' ]])) {
745
+ $ updatedEntry = $ entriesById [$ existingEntry ['value_id ' ]];
746
+ if ($ updatedEntry ['file ' ] === null ) {
747
+ unset($ updatedEntry ['file ' ]);
748
+ }
749
+ $ existingMediaGallery [$ key ] = array_merge ($ existingEntry , $ updatedEntry );
750
+ } else {
751
+ //set the removed flag
752
+ $ existingEntry ['removed ' ] = true ;
753
+ }
754
+ }
755
+
756
+ return $ existingMediaGallery ;
757
+ }
758
+
759
+ /**
760
+ * @param array $newEntries
761
+ * @param ProductInterface $product
762
+ * @param array $entriesById
763
+ * @return void
764
+ * @throws InputException
765
+ * @throws LocalizedException
766
+ * @throws StateException
767
+ */
768
+ private function processingNewEntries (array $ newEntries , ProductInterface $ product , array $ entriesById )
769
+ {
770
+ foreach ($ newEntries as $ newEntry ) {
771
+ if (!isset ($ newEntry ['content ' ])) {
772
+ throw new InputException (__ ('The image content is not valid. ' ));
773
+ }
774
+ /** @var ImageContentInterface $contentDataObject */
775
+ $ contentDataObject = $ this ->contentFactory ->create ()
776
+ ->setName ($ newEntry ['content ' ]['data ' ][ImageContentInterface::NAME ])
777
+ ->setBase64EncodedData ($ newEntry ['content ' ]['data ' ][ImageContentInterface::BASE64_ENCODED_DATA ])
778
+ ->setType ($ newEntry ['content ' ]['data ' ][ImageContentInterface::TYPE ]);
779
+ $ newEntry ['content ' ] = $ contentDataObject ;
780
+ $ this ->processNewMediaGalleryEntry ($ product , $ newEntry );
781
+
782
+ $ finalGallery = $ product ->getData ('media_gallery ' );
783
+ $ newEntryId = key (array_diff_key ($ product ->getData ('media_gallery ' )['images ' ], $ entriesById ));
784
+ $ newEntry = array_replace_recursive ($ newEntry , $ finalGallery ['images ' ][$ newEntryId ]);
785
+ $ entriesById [$ newEntryId ] = $ newEntry ;
786
+ $ finalGallery ['images ' ][$ newEntryId ] = $ newEntry ;
787
+ $ product ->setData ('media_gallery ' , $ finalGallery );
788
+ }
789
+ }
732
790
}
0 commit comments