Skip to content

Commit 069e838

Browse files
author
Yaroslav Onischenko
committed
Merge remote-tracking branch 'origin/MAGETWO-44267' into develop
2 parents c5ea78b + 010d9ab commit 069e838

File tree

4 files changed

+78
-44
lines changed

4 files changed

+78
-44
lines changed

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

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -122,21 +122,40 @@ public function getAddImagesButton()
122122
*/
123123
public function getImagesJson()
124124
{
125-
if (is_array($this->getElement()->getImages())) {
126-
$value = $this->getElement()->getImages();
125+
$value = $this->getElement()->getImages();
126+
if (is_array($value) &&
127+
array_key_exists('images', $value) &&
128+
is_array($value['images']) &&
129+
count($value['images'])
130+
) {
127131
$directory = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA);
128-
if (is_array($value['images']) && count($value['images']) > 0) {
129-
foreach ($value['images'] as &$image) {
130-
$image['url'] = $this->_mediaConfig->getMediaUrl($image['file']);
131-
$fileHandler = $directory->stat($this->_mediaConfig->getMediaPath($image['file']));
132-
$image['size'] = $fileHandler['size'];
133-
}
134-
return $this->_jsonEncoder->encode($value['images']);
132+
$images = $this->sortImagesByPosition($value['images']);
133+
foreach ($images as &$image) {
134+
$image['url'] = $this->_mediaConfig->getMediaUrl($image['file']);
135+
$fileHandler = $directory->stat($this->_mediaConfig->getMediaPath($image['file']));
136+
$image['size'] = $fileHandler['size'];
135137
}
138+
return $this->_jsonEncoder->encode($images);
136139
}
137140
return '[]';
138141
}
139142

143+
/**
144+
* Sort images array by position key
145+
*
146+
* @param array $images
147+
* @return array
148+
*/
149+
private function sortImagesByPosition($images)
150+
{
151+
if (is_array($images)) {
152+
usort($images, function ($imageA, $imageB) {
153+
return ($imageA['position'] < $imageB['position']) ? -1 : 1;
154+
});
155+
}
156+
return $images;
157+
}
158+
140159
/**
141160
* @return string
142161
*/

app/code/Magento/Catalog/Test/Unit/Block/Adminhtml/Product/Helper/Form/Gallery/ContentTest.php

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,37 +82,59 @@ public function testGetImagesJson()
8282
['file_1.jpg', 'catalog/product/image_1.jpg'],
8383
['file_2.jpg', 'catalog/product/image_2.jpg']
8484
];
85-
// @codingStandardsIgnoreStart
86-
$encodedString = '[{"value_id":"1","file":"image_1.jpg","media_type":"image","url":"http:\/\/magento2.dev\/pub\/media\/catalog\/product\/image_1.jpg","size":879394},{"value_id":"2","file":"image_2.jpg","media_type":"image","url":"http:\/\/magento2.dev\/pub\/media\/catalog\/product\/image`_2.jpg","size":399659}]';
87-
// @codingStandardsIgnoreEnd
85+
86+
$sizeMap = [
87+
['catalog/product/image_1.jpg', ['size' => 399659]],
88+
['catalog/product/image_2.jpg', ['size' => 879394]]
89+
];
90+
91+
$imagesResult = [
92+
[
93+
'value_id' => '2',
94+
'file' => 'file_2.jpg',
95+
'media_type' => 'image',
96+
'position' => '0',
97+
'url' => 'url_to_the_image/image_2.jpg',
98+
'size' => 879394
99+
],
100+
[
101+
'value_id' => '1',
102+
'file' => 'file_1.jpg',
103+
'media_type' => 'image',
104+
'position' => '1',
105+
'url' => 'url_to_the_image/image_1.jpg',
106+
'size' => 399659
107+
]
108+
];
109+
88110
$images = [
89111
'images' => [
90112
[
91113
'value_id' => '1',
92114
'file' => 'file_1.jpg',
93115
'media_type' => 'image',
116+
'position' => '1'
94117
] ,
95118
[
96119
'value_id' => '2',
97120
'file' => 'file_2.jpg',
98121
'media_type' => 'image',
122+
'position' => '0'
99123
]
100124
]
101125
];
102-
$firstStat = ['size' => 879394];
103-
$secondStat = ['size' => 399659];
126+
104127
$this->content->setElement($this->galleryMock);
105-
$this->galleryMock->expects($this->any())->method('getImages')->willReturn($images);
128+
$this->galleryMock->expects($this->once())->method('getImages')->willReturn($images);
106129
$this->fileSystemMock->expects($this->once())->method('getDirectoryRead')->willReturn($this->readMock);
107130

108131
$this->mediaConfigMock->expects($this->any())->method('getMediaUrl')->willReturnMap($url);
109-
$this->mediaConfigMock->expects($this->any())->method('getMediaPath')->willReturn($mediaPath);
132+
$this->mediaConfigMock->expects($this->any())->method('getMediaPath')->willReturnMap($mediaPath);
110133

111-
$this->readMock->expects($this->at(0))->method('stat')->willReturn($firstStat);
112-
$this->readMock->expects($this->at(1))->method('stat')->willReturn($secondStat);
113-
$this->jsonEncoderMock->expects($this->once())->method('encode')->willReturn($encodedString);
134+
$this->readMock->expects($this->any())->method('stat')->willReturnMap($sizeMap);
135+
$this->jsonEncoderMock->expects($this->once())->method('encode')->willReturnCallback('json_encode');
114136

115-
$this->assertSame($encodedString, $this->content->getImagesJson());
137+
$this->assertSame(json_encode($imagesResult), $this->content->getImagesJson());
116138
}
117139

118140
public function testGetImagesJsonWithoutImages()

app/code/Magento/ProductVideo/view/frontend/web/js/fotorama-add-video-events.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -249,11 +249,7 @@ define([
249249
tmpVideoData.videoUrl = tmpInputData.videoUrl;
250250
}
251251

252-
if (tmpVideoData.isBase) {
253-
videoData.unshift(tmpVideoData);
254-
} else {
255-
videoData.push(tmpVideoData);
256-
}
252+
videoData.push(tmpVideoData);
257253
}
258254

259255
return videoData;

lib/web/mage/gallery/gallery.js

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,20 @@ define([
1515
'use strict';
1616

1717
/**
18-
* Set main item first in order.
19-
* @param {Array.<Object>} data - Set of gallery items to update.
18+
* Retrieves index if the main item.
19+
* @param {Array.<Object>} data - Set of gallery items.
2020
*/
21-
var pushMainFirst = function (data) {
21+
var getMainImageIndex = function (data) {
2222
var mainIndex;
23-
24-
if (!_.every(data, function (item) {
23+
if (_.every(data, function (item) {
2524
return _.isObject(item);
2625
})
2726
) {
28-
return data;
29-
}
30-
31-
mainIndex = _.findIndex(data, function (item) {
32-
return item.isMain;
33-
});
34-
35-
if (mainIndex > -1) {
36-
data.unshift(data.splice(mainIndex, 1)[0]);
27+
mainIndex = _.findIndex(data, function (item) {
28+
return item.isMain;
29+
});
3730
}
38-
39-
return data;
31+
return mainIndex > 0 ? mainIndex : 0;
4032
},
4133

4234
/**
@@ -130,7 +122,7 @@ define([
130122
fotoramaApi: null,
131123
isFullscreen: false,
132124
api: null,
133-
data: _.clone(pushMainFirst(config.data))
125+
data: _.clone(config.data)
134126
};
135127
config.options.ratio = config.options.width / config.options.height;
136128
config.options.height = null;
@@ -270,7 +262,8 @@ define([
270262
tpl = template(galleryTpl, {
271263
next: $t('Next'),
272264
previous: $t('Previous')
273-
});
265+
}),
266+
mainImageIndex;
274267

275268
if (settings.breakpoints) {
276269
_.each(_.values(settings.breakpoints), function (breakpoint) {
@@ -297,6 +290,11 @@ define([
297290
settings.$elementF.fotorama(config);
298291
settings.fotoramaApi = settings.$elementF.data('fotorama');
299292
$.extend(true, config, this.startConfig);
293+
294+
mainImageIndex = getMainImageIndex(config.data);
295+
if (mainImageIndex) {
296+
this.settings.fotoramaApi.show(mainImageIndex);
297+
}
300298
},
301299

302300
/**
@@ -466,7 +464,6 @@ define([
466464
*/
467465
updateData: function (data) {
468466
if (_.isArray(data)) {
469-
pushMainFirst(data);
470467
settings.fotoramaApi.load(data);
471468

472469
$.extend(false, settings, {

0 commit comments

Comments
 (0)