33 * Copyright © Magento, Inc. All rights reserved.
44 * See COPYING.txt for license details.
55 */
6+
67namespace Magento \ProductVideo \Model \Plugin \Catalog \Product \Gallery ;
78
89use Magento \ProductVideo \Model \Product \Attribute \Media \ExternalVideoEntryConverter ;
@@ -30,14 +31,15 @@ public function beforeExecute(
3031 \Magento \Catalog \Model \Product $ product ,
3132 array $ arguments = []
3233 ) {
34+ /** @var \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attribute */
3335 $ attribute = $ mediaGalleryCreateHandler ->getAttribute ();
3436 $ mediaCollection = $ this ->getMediaEntriesDataCollection ($ product , $ attribute );
3537 if (!empty ($ mediaCollection )) {
3638 $ storeDataCollection = $ this ->loadStoreViewVideoData ($ mediaCollection , $ product ->getStoreId ());
3739 $ mediaCollection = $ this ->addAdditionalStoreData ($ mediaCollection , $ storeDataCollection );
3840 $ product ->setData (
3941 $ attribute ->getAttributeCode (),
40- $ mediaCollection + $ product -> getData ( $ attribute -> getAttributeCode ())
42+ $ mediaCollection
4143 );
4244 }
4345 }
@@ -57,6 +59,10 @@ public function afterExecute(
5759 );
5860
5961 if (!empty ($ mediaCollection )) {
62+ $ newVideoCollection = $ this ->collectNewVideos ($ mediaCollection );
63+ $ this ->saveVideoData ($ newVideoCollection , 0 );
64+ $ this ->saveAdditionalStoreData ($ newVideoCollection );
65+
6066 $ videoDataCollection = $ this ->collectVideoData ($ mediaCollection );
6167 $ this ->saveVideoData ($ videoDataCollection , $ product ->getStoreId ());
6268 $ this ->saveAdditionalStoreData ($ videoDataCollection );
@@ -168,10 +174,7 @@ protected function collectVideoData(array $mediaCollection)
168174 {
169175 $ videoDataCollection = [];
170176 foreach ($ mediaCollection as $ item ) {
171- if (!empty ($ item ['media_type ' ])
172- && empty ($ item ['removed ' ])
173- && $ item ['media_type ' ] == ExternalVideoEntryConverter::MEDIA_TYPE_CODE
174- ) {
177+ if ($ this ->isVideoItem ($ item )) {
175178 $ videoData = $ this ->extractVideoDataFromRowData ($ item );
176179 $ videoDataCollection [] = $ videoData ;
177180 }
@@ -200,11 +203,7 @@ protected function collectVideoEntriesIdsToAdditionalLoad(array $mediaCollection
200203 {
201204 $ ids = [];
202205 foreach ($ mediaCollection as $ item ) {
203- if (!empty ($ item ['media_type ' ])
204- && empty ($ item ['removed ' ])
205- && $ item ['media_type ' ] == ExternalVideoEntryConverter::MEDIA_TYPE_CODE
206- && isset ($ item ['save_data_from ' ])
207- ) {
206+ if ($ this ->isVideoItem ($ item ) && isset ($ item ['save_data_from ' ])) {
208207 $ ids [] = $ item ['save_data_from ' ];
209208 }
210209 }
@@ -216,26 +215,27 @@ protected function collectVideoEntriesIdsToAdditionalLoad(array $mediaCollection
216215 * @param array $data
217216 * @return array
218217 */
219- protected function addAdditionalStoreData (array $ mediaCollection , array $ data )
218+ protected function addAdditionalStoreData (array $ mediaCollection , array $ data ): array
220219 {
221- foreach ($ mediaCollection as &$ mediaItem ) {
220+ $ return = [];
221+ foreach ($ mediaCollection as $ key => $ mediaItem ) {
222222 if (!empty ($ mediaItem ['save_data_from ' ])) {
223223 $ additionalData = $ this ->createAdditionalStoreDataCollection ($ data , $ mediaItem ['save_data_from ' ]);
224224 if (!empty ($ additionalData )) {
225225 $ mediaItem [self ::ADDITIONAL_STORE_DATA_KEY ] = $ additionalData ;
226226 }
227227 }
228+ $ return [$ key ] = $ mediaItem ;
228229 }
229-
230- return ['images ' => $ mediaCollection ];
230+ return ['images ' => $ return ];
231231 }
232232
233233 /**
234234 * @param array $storeData
235235 * @param int $valueId
236236 * @return array
237237 */
238- protected function createAdditionalStoreDataCollection (array $ storeData , $ valueId )
238+ protected function createAdditionalStoreDataCollection (array $ storeData , $ valueId ): array
239239 {
240240 $ result = [];
241241 foreach ($ storeData as $ item ) {
@@ -247,4 +247,41 @@ protected function createAdditionalStoreDataCollection(array $storeData, $valueI
247247
248248 return $ result ;
249249 }
250+
251+ /**
252+ * @param array $mediaCollection
253+ * @return array
254+ */
255+ private function collectNewVideos (array $ mediaCollection ): array
256+ {
257+ $ return = [];
258+ foreach ($ mediaCollection as $ item ) {
259+ if ($ this ->isVideoItem ($ item ) && $ this ->isNewVideo ($ item )) {
260+ $ return [] = $ this ->extractVideoDataFromRowData ($ item );
261+ }
262+ }
263+ return $ return ;
264+ }
265+
266+ /**
267+ * @param $item
268+ * @return bool
269+ */
270+ private function isVideoItem ($ item ): bool
271+ {
272+ return !empty ($ item ['media_type ' ])
273+ && empty ($ item ['removed ' ])
274+ && $ item ['media_type ' ] == ExternalVideoEntryConverter::MEDIA_TYPE_CODE ;
275+ }
276+
277+ /**
278+ * @param $item
279+ * @return bool
280+ */
281+ private function isNewVideo ($ item ): bool
282+ {
283+ return !isset ($ item ['video_url_default ' ], $ item ['video_title_default ' ])
284+ || empty ($ item ['video_url_default ' ])
285+ || empty ($ item ['video_title_default ' ]);
286+ }
250287}
0 commit comments