11
11
*/
12
12
namespace Magento \Catalog \Block \Product \View ;
13
13
14
+ use Magento \Catalog \Block \Product \Context ;
14
15
use Magento \Catalog \Helper \Image ;
15
16
use Magento \Catalog \Model \Product ;
17
+ use Magento \Catalog \Model \Product \Gallery \ImagesConfigFactoryInterface ;
16
18
use Magento \Framework \Data \Collection ;
17
19
use Magento \Framework \DataObject ;
20
+ use Magento \Framework \App \ObjectManager ;
21
+ use Magento \Framework \Json \EncoderInterface ;
22
+ use Magento \Framework \Serialize \Serializer \Json ;
23
+ use Magento \Framework \Stdlib \ArrayUtils ;
18
24
19
- class Gallery extends \ Magento \ Catalog \ Block \ Product \ View \ AbstractView
25
+ class Gallery extends AbstractView
20
26
{
21
27
/**
22
28
* @var \Magento\Framework\Config\View
23
29
*/
24
30
protected $ configView ;
25
31
26
32
/**
27
- * @var \Magento\Framework\Json\ EncoderInterface
33
+ * @var EncoderInterface
28
34
*/
29
35
protected $ jsonEncoder ;
30
36
@@ -34,24 +40,30 @@ class Gallery extends \Magento\Catalog\Block\Product\View\AbstractView
34
40
protected $ galleryImagesConfig ;
35
41
36
42
/**
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
43
+ * @var ImagesConfigFactoryInterface
44
+ */
45
+ private $ galleryImagesConfigFactory ;
46
+
47
+ /**
48
+ * @param Context $context
49
+ * @param ArrayUtils $arrayUtils
50
+ * @param EncoderInterface $jsonEncoder
51
+ * @param ImagesConfigFactoryInterface $imagesConfigFactory
52
+ * @param array $galleryImagesConfig
53
+ * @param array $data
43
54
*/
44
55
public function __construct (
45
- \ Magento \ Catalog \ Block \ Product \ Context $ context ,
46
- \ Magento \ Framework \ Stdlib \ ArrayUtils $ arrayUtils ,
47
- \ Magento \ Framework \ Json \ EncoderInterface $ jsonEncoder ,
48
- \ Magento \ Catalog \ Model \ Product \ Gallery \ ImagesConfigFactoryInterface $ imagesConfigFactory ,
49
- array $ galleryImagesConfig = [] ,
50
- array $ data = []
56
+ Context $ context ,
57
+ ArrayUtils $ arrayUtils ,
58
+ EncoderInterface $ jsonEncoder ,
59
+ array $ data = [] ,
60
+ ImagesConfigFactoryInterface $ imagesConfigFactory = null ,
61
+ array $ galleryImagesConfig = []
51
62
) {
52
- $ this ->jsonEncoder = $ jsonEncoder ;
53
63
parent ::__construct ($ context , $ arrayUtils , $ data );
54
- $ this ->galleryImagesConfig = $ imagesConfigFactory ->create ($ galleryImagesConfig );
64
+ $ this ->jsonEncoder = $ jsonEncoder ?: ObjectManager::getInstance ()->get (Json::class);
65
+ $ this ->galleryImagesConfigFactory = $ imagesConfigFactory ?: ObjectManager::getInstance ()->get (ImagesConfigFactoryInterface::class);
66
+ $ this ->galleryImagesConfig = $ galleryImagesConfig ;
55
67
}
56
68
57
69
/**
@@ -65,7 +77,7 @@ public function getGalleryImages()
65
77
$ images = $ product ->getMediaGalleryImages ();
66
78
if ($ images instanceof \Magento \Framework \Data \Collection) {
67
79
foreach ($ images as $ image ) {
68
- foreach ($ this ->galleryImagesConfig ->getItems () as $ imageConfig ) {
80
+ foreach ($ this ->getGalleryImagesConfig () ->getItems () as $ imageConfig ) {
69
81
/** @var Product $product */
70
82
$ image ->setData (
71
83
$ imageConfig ->getData ('data_object_key ' ),
@@ -116,7 +128,7 @@ public function getGalleryImagesJson()
116
128
'position ' => $ image ->getData ('position ' ),
117
129
'isMain ' => $ this ->isMainImage ($ image ),
118
130
]);
119
- foreach ($ this ->galleryImagesConfig ->getItems () as $ imageConfig ) {
131
+ foreach ($ this ->getGalleryImagesConfig () ->getItems () as $ imageConfig ) {
120
132
$ imageItem ->setData (
121
133
$ imageConfig ->getData ('json_object_key ' ),
122
134
$ image ->getData ($ imageConfig ->getData ('data_object_key ' ))
@@ -189,4 +201,16 @@ private function getConfigView()
189
201
}
190
202
return $ this ->configView ;
191
203
}
204
+
205
+ /**
206
+ * @return Collection
207
+ */
208
+ private function getGalleryImagesConfig ()
209
+ {
210
+ if (false === $ this ->hasData ('gallery_images_config ' )) {
211
+ $ this ->setData ('gallery_images_config ' , $ this ->galleryImagesConfigFactory ->create ($ this ->galleryImagesConfig ));
212
+ }
213
+
214
+ return $ this ->getData ('gallery_images_config ' );
215
+ }
192
216
}
0 commit comments