Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Gallery.php
Go to the documentation of this file.
1 <?php
13 
24 
31 class Gallery extends AbstractView
32 {
36  protected $configView;
37 
41  protected $jsonEncoder;
42 
46  private $galleryImagesConfig;
47 
51  private $galleryImagesConfigFactory;
52 
56  private $imageUrlBuilder;
57 
67  public function __construct(
68  Context $context,
71  array $data = [],
72  ImagesConfigFactoryInterface $imagesConfigFactory = null,
73  array $galleryImagesConfig = [],
74  UrlBuilder $urlBuilder = null
75  ) {
76  parent::__construct($context, $arrayUtils, $data);
77  $this->jsonEncoder = $jsonEncoder;
78  $this->galleryImagesConfigFactory = $imagesConfigFactory ?: ObjectManager::getInstance()
79  ->get(ImagesConfigFactoryInterface::class);
80  $this->galleryImagesConfig = $galleryImagesConfig;
81  $this->imageUrlBuilder = $urlBuilder ?? ObjectManager::getInstance()->get(UrlBuilder::class);
82  }
83 
89  public function getGalleryImages()
90  {
91  $product = $this->getProduct();
92  $images = $product->getMediaGalleryImages();
93  if (!$images instanceof \Magento\Framework\Data\Collection) {
94  return $images;
95  }
96 
97  foreach ($images as $image) {
98  $galleryImagesConfig = $this->getGalleryImagesConfig()->getItems();
99  foreach ($galleryImagesConfig as $imageConfig) {
100  $image->setData(
101  $imageConfig->getData('data_object_key'),
102  $this->imageUrlBuilder->getUrl($image->getFile(), $imageConfig['image_id'])
103  );
104  }
105  }
106 
107  return $images;
108  }
109 
115  public function getMagnifier()
116  {
117  return $this->jsonEncoder->encode($this->getVar('magnifier'));
118  }
119 
125  public function getBreakpoints()
126  {
127  return $this->jsonEncoder->encode($this->getVar('breakpoints'));
128  }
129 
135  public function getGalleryImagesJson()
136  {
137  $imagesItems = [];
139  foreach ($this->getGalleryImages() as $image) {
140  $imageItem = new DataObject([
141  'thumb' => $image->getData('small_image_url'),
142  'img' => $image->getData('medium_image_url'),
143  'full' => $image->getData('large_image_url'),
144  'caption' => ($image->getLabel() ?: $this->getProduct()->getName()),
145  'position' => $image->getData('position'),
146  'isMain' => $this->isMainImage($image),
147  'type' => str_replace('external-', '', $image->getMediaType()),
148  'videoUrl' => $image->getVideoUrl(),
149  ]);
150  foreach ($this->getGalleryImagesConfig()->getItems() as $imageConfig) {
151  $imageItem->setData(
152  $imageConfig->getData('json_object_key'),
153  $image->getData($imageConfig->getData('data_object_key'))
154  );
155  }
156  $imagesItems[] = $imageItem->toArray();
157  }
158  if (empty($imagesItems)) {
159  $imagesItems[] = [
160  'thumb' => $this->_imageHelper->getDefaultPlaceholderUrl('thumbnail'),
161  'img' => $this->_imageHelper->getDefaultPlaceholderUrl('image'),
162  'full' => $this->_imageHelper->getDefaultPlaceholderUrl('image'),
163  'caption' => '',
164  'position' => '0',
165  'isMain' => true,
166  'type' => 'image',
167  'videoUrl' => null,
168  ];
169  }
170  return json_encode($imagesItems);
171  }
172 
179  public function getGalleryUrl($image = null)
180  {
181  $params = ['id' => $this->getProduct()->getId()];
182  if ($image) {
183  $params['image'] = $image->getValueId();
184  }
185  return $this->getUrl('catalog/product/gallery', $params);
186  }
187 
194  public function isMainImage($image)
195  {
196  $product = $this->getProduct();
197  return $product->getImage() == $image->getFile();
198  }
199 
208  public function getImageAttribute($imageId, $attributeName, $default = null)
209  {
210  $attributes =
211  $this->getConfigView()->getMediaAttributes('Magento_Catalog', Image::MEDIA_TYPE_CONFIG_NODE, $imageId);
212  return $attributes[$attributeName] ?? $default;
213  }
214 
220  private function getConfigView()
221  {
222  if (!$this->configView) {
223  $this->configView = $this->_viewConfig->getViewConfig();
224  }
225  return $this->configView;
226  }
227 
233  private function getGalleryImagesConfig()
234  {
235  if (false === $this->hasData('gallery_images_config')) {
236  $galleryImageConfig = $this->galleryImagesConfigFactory->create($this->galleryImagesConfig);
237  $this->setData('gallery_images_config', $galleryImageConfig);
238  }
239 
240  return $this->getData('gallery_images_config');
241  }
242 }
getData($key='', $index=null)
Definition: DataObject.php:119
$attributes
Definition: matrix.phtml:13
setData($key, $value=null)
Definition: DataObject.php:72
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18