Skip to content

Commit c4894fa

Browse files
David VerholenDavid Verholen
authored andcommitted
decouple product view gallery images config from Gallery Block
1 parent 9224419 commit c4894fa

File tree

7 files changed

+153
-40
lines changed

7 files changed

+153
-40
lines changed

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

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111
*/
1212
namespace Magento\Catalog\Block\Product\View;
1313

14-
use Magento\Framework\Data\Collection;
15-
use Magento\Framework\Json\EncoderInterface;
1614
use Magento\Catalog\Helper\Image;
15+
use Magento\Catalog\Model\Product;
16+
use Magento\Framework\Data\Collection;
17+
use Magento\Framework\DataObject;
1718

1819
class Gallery extends \Magento\Catalog\Block\Product\View\AbstractView
1920
{
@@ -28,19 +29,29 @@ class Gallery extends \Magento\Catalog\Block\Product\View\AbstractView
2829
protected $jsonEncoder;
2930

3031
/**
31-
* @param \Magento\Catalog\Block\Product\Context $context
32-
* @param \Magento\Framework\Stdlib\ArrayUtils $arrayUtils
33-
* @param EncoderInterface $jsonEncoder
34-
* @param array $data
32+
* @var DataObject
33+
*/
34+
protected $galleryImagesConfig;
35+
36+
/**
37+
* @param \Magento\Catalog\Block\Product\Context $context
38+
* @param \Magento\Framework\Stdlib\ArrayUtils $arrayUtils
39+
* @param \Magento\Framework\Json\EncoderInterface $jsonEncoder
40+
* @param \Magento\Catalog\Model\Product\Gallery\ImagesConfigFactoryInterface $imagesConfigFactory
41+
* @param array $galleryImagesConfig
42+
* @param array $data
3543
*/
3644
public function __construct(
3745
\Magento\Catalog\Block\Product\Context $context,
3846
\Magento\Framework\Stdlib\ArrayUtils $arrayUtils,
39-
EncoderInterface $jsonEncoder,
47+
\Magento\Framework\Json\EncoderInterface $jsonEncoder,
48+
\Magento\Catalog\Model\Product\Gallery\ImagesConfigFactoryInterface $imagesConfigFactory,
49+
array $galleryImagesConfig = [],
4050
array $data = []
4151
) {
4252
$this->jsonEncoder = $jsonEncoder;
4353
parent::__construct($context, $arrayUtils, $data);
54+
$this->galleryImagesConfig = $imagesConfigFactory->create($galleryImagesConfig);
4455
}
4556

4657
/**
@@ -54,25 +65,15 @@ public function getGalleryImages()
5465
$images = $product->getMediaGalleryImages();
5566
if ($images instanceof \Magento\Framework\Data\Collection) {
5667
foreach ($images as $image) {
57-
/* @var \Magento\Framework\DataObject $image */
58-
$image->setData(
59-
'small_image_url',
60-
$this->_imageHelper->init($product, 'product_page_image_small')
61-
->setImageFile($image->getFile())
62-
->getUrl()
63-
);
64-
$image->setData(
65-
'medium_image_url',
66-
$this->_imageHelper->init($product, 'product_page_image_medium_no_frame')
67-
->setImageFile($image->getFile())
68-
->getUrl()
69-
);
70-
$image->setData(
71-
'large_image_url',
72-
$this->_imageHelper->init($product, 'product_page_image_large_no_frame')
73-
->setImageFile($image->getFile())
74-
->getUrl()
75-
);
68+
/** @var DataObject $image */
69+
$this->galleryImagesConfig->walk(function (DataObject $imageConfig) use ($image, $product) {
70+
/** @var Product $product */
71+
$productImage = $this->_imageHelper->init($product,
72+
$imageConfig['image_id'])
73+
->setImageFile($image->getData('file'))
74+
->getUrl();
75+
$image->setData($imageConfig->getData('data_object_key'), $productImage);
76+
});
7677
}
7778
}
7879

@@ -107,15 +108,22 @@ public function getBreakpoints()
107108
public function getGalleryImagesJson()
108109
{
109110
$imagesItems = [];
111+
/** @var DataObject $image */
110112
foreach ($this->getGalleryImages() as $image) {
111-
$imagesItems[] = [
112-
'thumb' => $image->getData('small_image_url'),
113-
'img' => $image->getData('medium_image_url'),
114-
'full' => $image->getData('large_image_url'),
115-
'caption' => $image->getLabel(),
116-
'position' => $image->getPosition(),
117-
'isMain' => $this->isMainImage($image),
118-
];
113+
$imageItem = new DataObject([
114+
'caption' => $image->getData('label'),
115+
'position' => $image->getData('position'),
116+
'isMain' => $this->isMainImage($image),
117+
]);
118+
$this->galleryImagesConfig->walk(function (
119+
DataObject $imageConfig
120+
) use ($imageItem, $image) {
121+
$imageItem->setData(
122+
$imageConfig->getData('json_object_key'),
123+
$image->getData($imageConfig->getData('data_object_key'))
124+
);
125+
});
126+
$imagesItems[] = $imageItem->toArray();
119127
}
120128
if (empty($imagesItems)) {
121129
$imagesItems[] = [
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/**
3+
*
4+
* Copyright © 2015 Magento. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
namespace Magento\Catalog\Model\Product\Gallery;
8+
9+
use \Magento\Framework\Data\CollectionFactory;
10+
use Magento\Framework\DataObject;
11+
12+
class ImagesConfigFactory implements ImagesConfigFactoryInterface
13+
{
14+
/**
15+
* @var \Magento\Framework\Data\CollectionFactory
16+
*/
17+
protected $_dataCollectionFactory;
18+
19+
/**
20+
* ImagesConfigFactory constructor.
21+
*
22+
* @param CollectionFactory $_dataCollectionFactory
23+
*/
24+
public function __construct(CollectionFactory $_dataCollectionFactory)
25+
{
26+
$this->_dataCollectionFactory = $_dataCollectionFactory;
27+
}
28+
29+
/**
30+
* create Gallery Images Config Collection from array
31+
*
32+
* @param array $imagesConfig
33+
* @param array $data
34+
*
35+
* @return \Magento\Framework\Data\Collection
36+
*/
37+
public function create(array $imagesConfig, array $data = [])
38+
{
39+
/** @var \Magento\Framework\Data\Collection $collection */
40+
$collection = $this->_dataCollectionFactory->create($data);
41+
array_map(function($imageConfig) use ($collection) {
42+
$collection->addItem(new DataObject($imageConfig));
43+
}, $imagesConfig);
44+
45+
return $collection;
46+
}
47+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
/**
3+
*
4+
* Copyright © 2015 Magento. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
namespace Magento\Catalog\Model\Product\Gallery;
8+
9+
interface ImagesConfigFactoryInterface
10+
{
11+
/**
12+
* create Gallery Images Config Collection from array
13+
*
14+
* @param array $imagesConfig
15+
* @param array $data
16+
*
17+
* @return \Magento\Framework\Data\Collection
18+
*/
19+
public function create(array $imagesConfig, array $data = []);
20+
}

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ class GalleryTest extends \PHPUnit_Framework_TestCase
3737
*/
3838
protected $jsonEncoderMock;
3939

40+
/**
41+
* @var \Magento\Catalog\Model\Product\Gallery\ImagesConfigFactoryInterface|\PHPUnit_Framework_MockObject_MockObject
42+
*/
43+
protected $imagesConfigMock;
44+
4045
protected function setUp()
4146
{
4247
$this->mockContext();
@@ -49,10 +54,15 @@ protected function setUp()
4954
->disableOriginalConstructor()
5055
->getMock();
5156

57+
$this->imagesConfigMock = $this->getMockBuilder('Magento\Catalog\Model\Product\Gallery\ImagesConfigFactoryInterface')
58+
->disableOriginalConstructor()
59+
->getMock();
60+
5261
$this->model = new \Magento\Catalog\Block\Product\View\Gallery(
5362
$this->context,
5463
$this->arrayUtils,
55-
$this->jsonEncoderMock
64+
$this->jsonEncoderMock,
65+
$this->imagesConfigMock
5666
);
5767
}
5868

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
<preference for="Magento\Catalog\Api\SpecialPriceInterface" type="Magento\Catalog\Model\ResourceModel\Product\Price\SpecialPrice" />
6464
<preference for="Magento\Catalog\Model\ProductIdLocatorInterface" type="Magento\Catalog\Model\ProductIdLocator" />
6565
<preference for="Magento\Framework\Indexer\BatchProviderInterface" type="Magento\Framework\Indexer\BatchProvider" />
66+
<preference for="\Magento\Catalog\Model\Product\Gallery\ImagesConfigFactoryInterface" type="\Magento\Catalog\Model\Product\Gallery\ImagesConfigFactory" />
6667
<type name="Magento\Customer\Model\ResourceModel\Visitor">
6768
<plugin name="catalogLog" type="Magento\Catalog\Model\Plugin\Log" />
6869
</type>

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,25 @@
6060
<type name="Magento\Framework\Pricing\Render\PriceBox">
6161
<plugin name="catalog_price_box_key" type="Magento\Catalog\Block\Category\Plugin\PriceBoxTags" />
6262
</type>
63+
<type name="Magento\Catalog\Block\Product\View\Gallery">
64+
<arguments>
65+
<argument name="galleryImagesConfig" xsi:type="array">
66+
<item name="small_image" xsi:type="array">
67+
<item name="image_id" xsi:type="string">product_page_image_small</item>
68+
<item name="data_object_key" xsi:type="string">small_image_url</item>
69+
<item name="json_object_key" xsi:type="string">thumb</item>
70+
</item>
71+
<item name="medium_image" xsi:type="array">
72+
<item name="image_id" xsi:type="string">product_page_image_medium</item>
73+
<item name="data_object_key" xsi:type="string">medium_image_url</item>
74+
<item name="json_object_key" xsi:type="string">img</item>
75+
</item>
76+
<item name="large_image" xsi:type="array">
77+
<item name="image_id" xsi:type="string">product_page_image_large</item>
78+
<item name="data_object_key" xsi:type="string">large_image_url</item>
79+
<item name="json_object_key" xsi:type="string">full</item>
80+
</item>
81+
</argument>
82+
</arguments>
83+
</type>
6384
</config>

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,29 @@ class Gallery extends \Magento\Catalog\Block\Product\View\Gallery
1919
protected $mediaHelper;
2020

2121
/**
22-
* @param \Magento\Catalog\Block\Product\Context $context
23-
* @param \Magento\Framework\Stdlib\ArrayUtils $arrayUtils
24-
* @param \Magento\ProductVideo\Helper\Media $mediaHelper
25-
* @param \Magento\Framework\Json\EncoderInterface $jsonEncoder
26-
* @param array $data
22+
* @param \Magento\Catalog\Block\Product\Context $context
23+
* @param \Magento\Framework\Stdlib\ArrayUtils $arrayUtils
24+
* @param \Magento\Framework\Json\EncoderInterface $jsonEncoder
25+
* @param \Magento\ProductVideo\Helper\Media $mediaHelper
26+
* @param \Magento\Catalog\Model\Product\Gallery\ImagesConfigFactoryInterface $imagesConfigFactory
27+
* @param array $galleryImagesConfig
28+
* @param array $data
2729
*/
2830
public function __construct(
2931
\Magento\Catalog\Block\Product\Context $context,
3032
\Magento\Framework\Stdlib\ArrayUtils $arrayUtils,
3133
\Magento\Framework\Json\EncoderInterface $jsonEncoder,
3234
\Magento\ProductVideo\Helper\Media $mediaHelper,
35+
\Magento\Catalog\Model\Product\Gallery\ImagesConfigFactoryInterface $imagesConfigFactory,
36+
array $galleryImagesConfig = [],
3337
array $data = []
3438
) {
3539
parent::__construct(
3640
$context,
3741
$arrayUtils,
3842
$jsonEncoder,
43+
$imagesConfigFactory,
44+
$galleryImagesConfig,
3945
$data
4046
);
4147
$this->mediaHelper = $mediaHelper;

0 commit comments

Comments
 (0)