Skip to content

Commit 2f4fb95

Browse files
committed
Merge remote-tracking branch 'l3/MC-38951' into BUGFIX-11-23
2 parents 090c4a7 + f3e09ef commit 2f4fb95

File tree

11 files changed

+510
-22
lines changed

11 files changed

+510
-22
lines changed

app/code/Magento/Catalog/Block/Adminhtml/Product/Helper/Form/Gallery/Content.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,15 +209,22 @@ public function getImagesJson()
209209
*/
210210
private function sortImagesByPosition($images)
211211
{
212-
if (is_array($images)) {
212+
$nullPositions = [];
213+
foreach ($images as $index => $image) {
214+
if ($image['position'] === null) {
215+
$nullPositions[] = $image;
216+
unset($images[$index]);
217+
}
218+
}
219+
if (is_array($images) && !empty($images)) {
213220
usort(
214221
$images,
215222
function ($imageA, $imageB) {
216223
return ($imageA['position'] < $imageB['position']) ? -1 : 1;
217224
}
218225
);
219226
}
220-
return $images;
227+
return array_merge($images, $nullPositions);
221228
}
222229

223230
/**

app/code/Magento/Catalog/Model/Product/Gallery/CreateHandler.php

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -267,29 +267,50 @@ protected function processNewAndExistingImages($product, array &$images)
267267
{
268268
foreach ($images as &$image) {
269269
if (empty($image['removed'])) {
270+
$isNew = empty($image['value_id']);
270271
$data = $this->processNewImage($product, $image);
271272

272-
if (!$product->isObjectNew()) {
273-
$this->resourceModel->deleteGalleryValueInStore(
274-
$image['value_id'],
275-
$product->getData($this->metadata->getLinkField()),
276-
$product->getStoreId()
277-
);
278-
}
279273
// Add per store labels, position, disabled
280274
$data['value_id'] = $image['value_id'];
281275
$data['label'] = isset($image['label']) ? $image['label'] : '';
282-
$data['position'] = isset($image['position']) ? (int)$image['position'] : 0;
276+
$data['position'] = isset($image['position']) && $image['position'] !== ''
277+
? (int)$image['position']
278+
: null;
283279
$data['disabled'] = isset($image['disabled']) ? (int)$image['disabled'] : 0;
284280
$data['store_id'] = (int)$product->getStoreId();
285281

286282
$data[$this->metadata->getLinkField()] = (int)$product->getData($this->metadata->getLinkField());
287283

288-
$this->resourceModel->insertGalleryValueInStore($data);
284+
$this->saveGalleryStoreValue($product, $data);
285+
if ($isNew && $data['store_id'] !== Store::DEFAULT_STORE_ID) {
286+
$dataForDefaultScope = $data;
287+
$dataForDefaultScope['store_id'] = Store::DEFAULT_STORE_ID;
288+
$dataForDefaultScope['disabled'] = 0;
289+
$dataForDefaultScope['label'] = null;
290+
$this->saveGalleryStoreValue($product, $dataForDefaultScope);
291+
}
289292
}
290293
}
291294
}
292295

296+
/**
297+
* Save media gallery store value
298+
*
299+
* @param Product $product
300+
* @param array $data
301+
*/
302+
private function saveGalleryStoreValue(Product $product, array $data): void
303+
{
304+
if (!$product->isObjectNew()) {
305+
$this->resourceModel->deleteGalleryValueInStore(
306+
$data['value_id'],
307+
$data[$this->metadata->getLinkField()],
308+
$data['store_id']
309+
);
310+
}
311+
$this->resourceModel->insertGalleryValueInStore($data);
312+
}
313+
293314
/**
294315
* Processes image as new.
295316
*

app/code/Magento/Catalog/Model/Product/Gallery/ReadHandler.php

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ public function execute($entity, $arguments = [])
6464

6565
$this->addMediaDataToProduct(
6666
$entity,
67-
$mediaEntries
67+
$this->sortMediaEntriesByPosition($mediaEntries)
6868
);
69-
69+
7070
return $entity;
7171
}
7272

@@ -108,7 +108,7 @@ public function getAttribute()
108108
* Find default value
109109
*
110110
* @param string $key
111-
* @param string[] &$image
111+
* @param string[] $image
112112
* @return string
113113
* @deprecated 101.0.1
114114
* @since 101.0.0
@@ -121,4 +121,30 @@ protected function findDefaultValue($key, &$image)
121121

122122
return '';
123123
}
124+
125+
/**
126+
* Sort media entries by position
127+
*
128+
* @param array $mediaEntries
129+
* @return array
130+
*/
131+
private function sortMediaEntriesByPosition(array $mediaEntries): array
132+
{
133+
$mediaEntriesWithNullPositions = [];
134+
foreach ($mediaEntries as $index => $mediaEntry) {
135+
if ($mediaEntry['position'] === null) {
136+
$mediaEntriesWithNullPositions[] = $mediaEntry;
137+
unset($mediaEntries[$index]);
138+
}
139+
}
140+
if (!empty($mediaEntries)) {
141+
usort(
142+
$mediaEntries,
143+
function ($entryA, $entryB) {
144+
return ($entryA['position'] < $entryB['position']) ? -1 : 1;
145+
}
146+
);
147+
}
148+
return array_merge($mediaEntries, $mediaEntriesWithNullPositions);
149+
}
124150
}

app/code/Magento/CatalogImportExport/Model/Import/Product.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1807,7 +1807,7 @@ protected function _saveProducts()
18071807
if ($column === self::COL_MEDIA_IMAGE) {
18081808
$rowData[$column][] = $uploadedFile;
18091809
}
1810-
$mediaGallery[$storeId][$rowSku][$uploadedFile] = [
1810+
$mediaGalleryStoreData = [
18111811
'attribute_id' => $this->getMediaGalleryAttributeId(),
18121812
'label' => isset($rowLabels[$column][$columnImageKey])
18131813
? $rowLabels[$column][$columnImageKey]
@@ -1817,6 +1817,15 @@ protected function _saveProducts()
18171817
? $imageHiddenStates[$columnImage] : '0',
18181818
'value' => $uploadedFile,
18191819
];
1820+
$mediaGallery[$storeId][$rowSku][$uploadedFile] = $mediaGalleryStoreData;
1821+
// Add record for default scope if it does not exist
1822+
if (!($mediaGallery[Store::DEFAULT_STORE_ID][$rowSku][$uploadedFile] ?? [])) {
1823+
//Set label and disabled values to their default values
1824+
$mediaGalleryStoreData['label'] = null;
1825+
$mediaGalleryStoreData['disabled'] = 0;
1826+
$mediaGallery[Store::DEFAULT_STORE_ID][$rowSku][$uploadedFile] = $mediaGalleryStoreData;
1827+
}
1828+
18201829
}
18211830
}
18221831
}

app/code/Magento/CatalogImportExport/Model/Import/Product/MediaGalleryProcessor.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Magento\Framework\App\ResourceConnection;
1313
use Magento\Framework\EntityManager\MetadataPool;
1414
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingErrorAggregatorInterface;
15+
use Magento\Store\Model\Store;
1516

1617
/**
1718
* Process and saves images during import.
@@ -259,7 +260,10 @@ private function prepareMediaGalleryValueData(
259260
$position = $data['position'];
260261
$storeId = $data['store_id'];
261262
$mediaGalleryValueData[$index]['value_id'] = $productIdMediaValueIdMap[$productId][$value];
262-
$mediaGalleryValueData[$index]['position'] = $position + ($lastPositions[$storeId][$productId] ?? 0);
263+
$lastPosition = $lastPositions[$storeId][$productId]
264+
?? $lastPositions[Store::DEFAULT_STORE_ID][$productId]
265+
?? 0;
266+
$mediaGalleryValueData[$index]['position'] = $position + $lastPosition;
263267
unset($mediaGalleryValueData[$index]['value']);
264268
}
265269
return $mediaGalleryValueData;

dev/tests/api-functional/testsuite/Magento/Catalog/Api/ProductAttributeMediaGalleryManagementInterfaceTest.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,8 @@ public function testCreateWithNotDefaultStoreId()
235235
$this->assertEquals($updatedImage['file'], $targetProduct->getData('image'));
236236
// No values for default store view were provided
237237
$this->assertNull($updatedImage['label_default']);
238-
$this->assertNull($updatedImage['position_default']);
239-
$this->assertNull($updatedImage['disabled_default']);
238+
$this->assertEquals(1, $updatedImage['position_default']);
239+
$this->assertEquals(0, $updatedImage['disabled_default']);
240240
}
241241

242242
/**
@@ -483,7 +483,9 @@ public function testCreateThrowsExceptionIfProvidedImageHasWrongMimeType()
483483
public function testCreateThrowsExceptionIfTargetProductDoesNotExist()
484484
{
485485
$this->expectException(\Exception::class);
486-
$this->expectExceptionMessage('The product that was requested doesn\'t exist. Verify the product and try again.');
486+
$this->expectExceptionMessage(
487+
'The product that was requested doesn\'t exist. Verify the product and try again.'
488+
);
487489

488490
$this->createServiceInfo['rest']['resourcePath'] = '/V1/products/wrong_product_sku/media';
489491

@@ -538,7 +540,9 @@ public function testCreateThrowsExceptionIfProvidedImageNameContainsForbiddenCha
538540
public function testUpdateThrowsExceptionIfTargetProductDoesNotExist()
539541
{
540542
$this->expectException(\Exception::class);
541-
$this->expectExceptionMessage('The product that was requested doesn\'t exist. Verify the product and try again.');
543+
$this->expectExceptionMessage(
544+
'The product that was requested doesn\'t exist. Verify the product and try again.'
545+
);
542546

543547
$this->updateServiceInfo['rest']['resourcePath'] = '/V1/products/wrong_product_sku/media'
544548
. '/' . 'wrong-sku';
@@ -592,7 +596,9 @@ public function testUpdateThrowsExceptionIfThereIsNoImageWithGivenId()
592596
public function testDeleteThrowsExceptionIfTargetProductDoesNotExist()
593597
{
594598
$this->expectException(\Exception::class);
595-
$this->expectExceptionMessage('The product that was requested doesn\'t exist. Verify the product and try again.');
599+
$this->expectExceptionMessage(
600+
'The product that was requested doesn\'t exist. Verify the product and try again.'
601+
);
596602

597603
$this->deleteServiceInfo['rest']['resourcePath'] = '/V1/products/wrong_product_sku/media/9999';
598604
$requestData = [
@@ -782,6 +788,6 @@ public function testAddProductVideo()
782788
$this->assertEquals(1, $updatedImage['position']);
783789
$this->assertEquals(0, $updatedImage['disabled']);
784790
$this->assertStringStartsWith('/t/e/test_image', $updatedImage['file']);
785-
$this->assertEquals($videoContent, array_intersect($updatedImage, $videoContent));
791+
$this->assertEquals($videoContent, array_intersect_key($updatedImage, $videoContent));
786792
}
787793
}

0 commit comments

Comments
 (0)