Skip to content

Commit 58bc916

Browse files
committed
MAGETWO-70878: Category position does not save
2 parents e443031 + e2b4d6b commit 58bc916

File tree

7 files changed

+231
-39
lines changed

7 files changed

+231
-39
lines changed

app/code/Magento/Catalog/Block/Product/View/Gallery.php

Lines changed: 68 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -11,39 +11,62 @@
1111
*/
1212
namespace Magento\Catalog\Block\Product\View;
1313

14+
use Magento\Catalog\Block\Product\Context;
15+
use Magento\Catalog\Helper\Image;
16+
use Magento\Catalog\Model\Product;
17+
use Magento\Catalog\Model\Product\Gallery\ImagesConfigFactoryInterface;
1418
use Magento\Framework\Data\Collection;
19+
use Magento\Framework\DataObject;
20+
use Magento\Framework\App\ObjectManager;
1521
use Magento\Framework\Json\EncoderInterface;
16-
use Magento\Catalog\Helper\Image;
22+
use Magento\Framework\Stdlib\ArrayUtils;
1723

1824
/**
1925
* @api
2026
*/
21-
class Gallery extends \Magento\Catalog\Block\Product\View\AbstractView
27+
class Gallery extends AbstractView
2228
{
2329
/**
2430
* @var \Magento\Framework\Config\View
2531
*/
2632
protected $configView;
2733

2834
/**
29-
* @var \Magento\Framework\Json\EncoderInterface
35+
* @var EncoderInterface
3036
*/
3137
protected $jsonEncoder;
3238

3339
/**
34-
* @param \Magento\Catalog\Block\Product\Context $context
35-
* @param \Magento\Framework\Stdlib\ArrayUtils $arrayUtils
40+
* @var array
41+
*/
42+
private $galleryImagesConfig;
43+
44+
/**
45+
* @var ImagesConfigFactoryInterface
46+
*/
47+
private $galleryImagesConfigFactory;
48+
49+
/**
50+
* @param Context $context
51+
* @param ArrayUtils $arrayUtils
3652
* @param EncoderInterface $jsonEncoder
3753
* @param array $data
54+
* @param ImagesConfigFactoryInterface $imagesConfigFactory
55+
* @param array $galleryImagesConfig
3856
*/
3957
public function __construct(
40-
\Magento\Catalog\Block\Product\Context $context,
41-
\Magento\Framework\Stdlib\ArrayUtils $arrayUtils,
58+
Context $context,
59+
ArrayUtils $arrayUtils,
4260
EncoderInterface $jsonEncoder,
43-
array $data = []
61+
array $data = [],
62+
ImagesConfigFactoryInterface $imagesConfigFactory = null,
63+
array $galleryImagesConfig = []
4464
) {
45-
$this->jsonEncoder = $jsonEncoder;
4665
parent::__construct($context, $arrayUtils, $data);
66+
$this->jsonEncoder = $jsonEncoder;
67+
$this->galleryImagesConfigFactory = $imagesConfigFactory ?: ObjectManager::getInstance()
68+
->get(ImagesConfigFactoryInterface::class);
69+
$this->galleryImagesConfig = $galleryImagesConfig;
4770
}
4871

4972
/**
@@ -55,25 +78,17 @@ public function getGalleryImages()
5578
{
5679
$product = $this->getProduct();
5780
$images = $product->getMediaGalleryImages();
58-
if ($images instanceof \Magento\Framework\Data\Collection) {
59-
foreach ($images as $image) {
60-
/* @var \Magento\Framework\DataObject $image */
61-
$image->setData(
62-
'small_image_url',
63-
$this->_imageHelper->init($product, 'product_page_image_small')
64-
->setImageFile($image->getFile())
65-
->getUrl()
66-
);
67-
$image->setData(
68-
'medium_image_url',
69-
$this->_imageHelper->init($product, 'product_page_image_medium_no_frame')
70-
->setImageFile($image->getFile())
71-
->getUrl()
72-
);
81+
if (!$images instanceof \Magento\Framework\Data\Collection) {
82+
return $images;
83+
}
84+
85+
foreach ($images as $image) {
86+
$galleryImagesConfig = $this->getGalleryImagesConfig()->getItems();
87+
foreach ($galleryImagesConfig as $imageConfig) {
7388
$image->setData(
74-
'large_image_url',
75-
$this->_imageHelper->init($product, 'product_page_image_large_no_frame')
76-
->setImageFile($image->getFile())
89+
$imageConfig->getData('data_object_key'),
90+
$this->_imageHelper->init($product, $imageConfig['image_id'])
91+
->setImageFile($image->getData('file'))
7792
->getUrl()
7893
);
7994
}
@@ -110,15 +125,20 @@ public function getBreakpoints()
110125
public function getGalleryImagesJson()
111126
{
112127
$imagesItems = [];
128+
/** @var DataObject $image */
113129
foreach ($this->getGalleryImages() as $image) {
114-
$imagesItems[] = [
115-
'thumb' => $image->getData('small_image_url'),
116-
'img' => $image->getData('medium_image_url'),
117-
'full' => $image->getData('large_image_url'),
118-
'caption' => $image->getLabel(),
119-
'position' => $image->getPosition(),
120-
'isMain' => $this->isMainImage($image),
121-
];
130+
$imageItem = new DataObject([
131+
'caption' => $image->getData('label'),
132+
'position' => $image->getData('position'),
133+
'isMain' => $this->isMainImage($image),
134+
]);
135+
foreach ($this->getGalleryImagesConfig()->getItems() as $imageConfig) {
136+
$imageItem->setData(
137+
$imageConfig->getData('json_object_key'),
138+
$image->getData($imageConfig->getData('data_object_key'))
139+
);
140+
}
141+
$imagesItems[] = $imageItem->toArray();
122142
}
123143
if (empty($imagesItems)) {
124144
$imagesItems[] = [
@@ -185,4 +205,17 @@ private function getConfigView()
185205
}
186206
return $this->configView;
187207
}
208+
209+
/**
210+
* @return Collection
211+
*/
212+
private function getGalleryImagesConfig()
213+
{
214+
if (false === $this->hasData('gallery_images_config')) {
215+
$galleryImageConfig = $this->galleryImagesConfigFactory->create($this->galleryImagesConfig);
216+
$this->setData('gallery_images_config', $galleryImageConfig);
217+
}
218+
219+
return $this->getData('gallery_images_config');
220+
}
188221
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Catalog\Model\Product\Gallery;
7+
8+
use Magento\Framework\Data\CollectionFactory;
9+
use Magento\Framework\DataObject;
10+
11+
class ImagesConfigFactory implements ImagesConfigFactoryInterface
12+
{
13+
/**
14+
* @var CollectionFactory
15+
*/
16+
private $dataCollectionFactory;
17+
18+
/**
19+
* @param CollectionFactory $dataCollectionFactory
20+
*/
21+
public function __construct(CollectionFactory $dataCollectionFactory)
22+
{
23+
$this->dataCollectionFactory = $dataCollectionFactory;
24+
}
25+
26+
/**
27+
* @inheritdoc
28+
*/
29+
public function create(array $imagesConfig, array $data = [])
30+
{
31+
/** @var \Magento\Framework\Data\Collection $collection */
32+
$collection = $this->dataCollectionFactory->create($data);
33+
array_map(function ($imageConfig) use ($collection) {
34+
$collection->addItem(new DataObject($imageConfig));
35+
}, $imagesConfig);
36+
37+
return $collection;
38+
}
39+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Catalog\Model\Product\Gallery;
7+
8+
interface ImagesConfigFactoryInterface
9+
{
10+
/**
11+
* Create Gallery Images Config Collection from array
12+
*
13+
* @param array $imagesConfig
14+
* @param array $data
15+
* @return \Magento\Framework\Data\Collection
16+
*/
17+
public function create(array $imagesConfig, array $data = []);
18+
}

app/code/Magento/Catalog/Test/Unit/Block/Product/View/GalleryTest.php

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
*/
66
namespace Magento\Catalog\Test\Unit\Block\Product\View;
77

8+
/**
9+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
10+
*/
811
class GalleryTest extends \PHPUnit_Framework_TestCase
912
{
1013
/**
@@ -37,6 +40,16 @@ class GalleryTest extends \PHPUnit_Framework_TestCase
3740
*/
3841
protected $jsonEncoderMock;
3942

43+
/**
44+
* @var \Magento\Catalog\Model\Product\Gallery\ImagesConfigFactoryInterface|\PHPUnit_Framework_MockObject_MockObject
45+
*/
46+
protected $imagesConfigFactoryMock;
47+
48+
/**
49+
* @var \Magento\Framework\Data\Collection|\PHPUnit_Framework_MockObject_MockObject
50+
*/
51+
protected $galleryImagesConfigMock;
52+
4053
protected function setUp()
4154
{
4255
$this->mockContext();
@@ -49,10 +62,14 @@ protected function setUp()
4962
->disableOriginalConstructor()
5063
->getMock();
5164

65+
$this->imagesConfigFactoryMock = $this->getImagesConfigFactory();
66+
5267
$this->model = new \Magento\Catalog\Block\Product\View\Gallery(
5368
$this->context,
5469
$this->arrayUtils,
55-
$this->jsonEncoderMock
70+
$this->jsonEncoderMock,
71+
[],
72+
$this->imagesConfigFactoryMock
5673
);
5774
}
5875

@@ -107,6 +124,10 @@ public function testGetGalleryImages()
107124
->with('product')
108125
->willReturn($productMock);
109126

127+
$this->galleryImagesConfigMock->expects($this->exactly(1))
128+
->method('getItems')
129+
->willReturn($this->getGalleryImagesConfigItems());
130+
110131
$this->imageHelper->expects($this->exactly(3))
111132
->method('init')
112133
->willReturnMap([
@@ -154,4 +175,57 @@ private function getImagesCollection()
154175

155176
return $collectionMock;
156177
}
178+
179+
/**
180+
* getImagesConfigFactory
181+
*
182+
* @return \Magento\Catalog\Model\Product\Gallery\ImagesConfigFactoryInterface
183+
*/
184+
private function getImagesConfigFactory()
185+
{
186+
$this->galleryImagesConfigMock = $this->getMockBuilder(\Magento\Framework\Data\Collection::class)
187+
->disableOriginalConstructor()
188+
->getMock();
189+
190+
$this->galleryImagesConfigMock->expects($this->any())
191+
->method('getIterator')
192+
->willReturn(new \ArrayIterator($this->getGalleryImagesConfigItems()));
193+
194+
$galleryImagesConfigFactoryMock = $this
195+
->getMockBuilder(\Magento\Catalog\Model\Product\Gallery\ImagesConfigFactoryInterface::class)
196+
->disableOriginalConstructor()
197+
->getMock();
198+
199+
$galleryImagesConfigFactoryMock->expects($this->any())
200+
->method('create')
201+
->willReturn($this->galleryImagesConfigMock);
202+
203+
return $galleryImagesConfigFactoryMock;
204+
}
205+
206+
/**
207+
* getGalleryImagesConfigItems
208+
*
209+
* @return array
210+
*/
211+
private function getGalleryImagesConfigItems()
212+
{
213+
return [
214+
new \Magento\Framework\DataObject([
215+
'image_id' => 'product_page_image_small',
216+
'data_object_key' => 'small_image_url',
217+
'json_object_key' => 'thumb'
218+
]),
219+
new \Magento\Framework\DataObject([
220+
'image_id' => 'product_page_image_medium',
221+
'data_object_key' => 'medium_image_url',
222+
'json_object_key' => 'img'
223+
]),
224+
new \Magento\Framework\DataObject([
225+
'image_id' => 'product_page_image_large',
226+
'data_object_key' => 'large_image_url',
227+
'json_object_key' => 'full'
228+
])
229+
];
230+
}
157231
}

app/code/Magento/Catalog/etc/di.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
<preference for="Magento\Catalog\Api\Data\ProductRender\PriceInfoInterface" type="Magento\Catalog\Model\ProductRender\PriceInfo" />
7070
<preference for="Magento\Catalog\Api\Data\ProductRender\FormattedPriceInfoInterface" type="Magento\Catalog\Model\ProductRender\FormattedPriceInfo" />
7171
<preference for="Magento\Framework\Indexer\BatchProviderInterface" type="Magento\Framework\Indexer\BatchProvider" />
72+
<preference for="\Magento\Catalog\Model\Product\Gallery\ImagesConfigFactoryInterface" type="\Magento\Catalog\Model\Product\Gallery\ImagesConfigFactory" />
7273
<type name="Magento\Customer\Model\ResourceModel\Visitor">
7374
<plugin name="catalogLog" type="Magento\Catalog\Model\Plugin\Log" />
7475
</type>

app/code/Magento/Catalog/etc/frontend/di.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,25 @@
7979
<argument name="typeId" xsi:type="string">recently_compared_product</argument>
8080
</arguments>
8181
</virtualType>
82+
<type name="Magento\Catalog\Block\Product\View\Gallery">
83+
<arguments>
84+
<argument name="galleryImagesConfig" xsi:type="array">
85+
<item name="small_image" xsi:type="array">
86+
<item name="image_id" xsi:type="string">product_page_image_small</item>
87+
<item name="data_object_key" xsi:type="string">small_image_url</item>
88+
<item name="json_object_key" xsi:type="string">thumb</item>
89+
</item>
90+
<item name="medium_image" xsi:type="array">
91+
<item name="image_id" xsi:type="string">product_page_image_medium</item>
92+
<item name="data_object_key" xsi:type="string">medium_image_url</item>
93+
<item name="json_object_key" xsi:type="string">img</item>
94+
</item>
95+
<item name="large_image" xsi:type="array">
96+
<item name="image_id" xsi:type="string">product_page_image_large</item>
97+
<item name="data_object_key" xsi:type="string">large_image_url</item>
98+
<item name="json_object_key" xsi:type="string">full</item>
99+
</item>
100+
</argument>
101+
</arguments>
102+
</type>
82103
</config>

app/code/Magento/ProductVideo/Block/Product/View/Gallery.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,28 @@ class Gallery extends \Magento\Catalog\Block\Product\View\Gallery
2424
/**
2525
* @param \Magento\Catalog\Block\Product\Context $context
2626
* @param \Magento\Framework\Stdlib\ArrayUtils $arrayUtils
27-
* @param \Magento\ProductVideo\Helper\Media $mediaHelper
2827
* @param \Magento\Framework\Json\EncoderInterface $jsonEncoder
28+
* @param \Magento\ProductVideo\Helper\Media $mediaHelper
2929
* @param array $data
30+
* @param \Magento\Catalog\Model\Product\Gallery\ImagesConfigFactoryInterface $imagesConfigFactory
31+
* @param array $galleryImagesConfig
3032
*/
3133
public function __construct(
3234
\Magento\Catalog\Block\Product\Context $context,
3335
\Magento\Framework\Stdlib\ArrayUtils $arrayUtils,
3436
\Magento\Framework\Json\EncoderInterface $jsonEncoder,
3537
\Magento\ProductVideo\Helper\Media $mediaHelper,
36-
array $data = []
38+
array $data = [],
39+
\Magento\Catalog\Model\Product\Gallery\ImagesConfigFactoryInterface $imagesConfigFactory = null,
40+
array $galleryImagesConfig = []
3741
) {
3842
parent::__construct(
3943
$context,
4044
$arrayUtils,
4145
$jsonEncoder,
42-
$data
46+
$data,
47+
$imagesConfigFactory,
48+
$galleryImagesConfig
4349
);
4450
$this->mediaHelper = $mediaHelper;
4551
}

0 commit comments

Comments
 (0)