|
5 | 5 | */ |
6 | 6 | namespace Magento\Swatches\Helper; |
7 | 7 |
|
| 8 | +use Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface; |
8 | 9 | use Magento\Catalog\Api\Data\ProductInterface as Product; |
9 | 10 | use Magento\Catalog\Api\ProductRepositoryInterface; |
10 | | -use Magento\Catalog\Helper\Image; |
11 | 11 | use Magento\Catalog\Model\Product as ModelProduct; |
12 | 12 | use Magento\Catalog\Model\Product\Image\UrlBuilder; |
13 | 13 | use Magento\Catalog\Model\ResourceModel\Eav\Attribute; |
@@ -324,35 +324,61 @@ private function addFilterByParent(ProductCollection $productCollection, $parent |
324 | 324 | * @param ModelProduct $product |
325 | 325 | * |
326 | 326 | * @return array |
| 327 | + * @throws \Magento\Framework\Exception\LocalizedException |
327 | 328 | */ |
328 | | - public function getProductMediaGallery(ModelProduct $product) |
| 329 | + public function getProductMediaGallery(ModelProduct $product): array |
329 | 330 | { |
330 | 331 | $baseImage = null; |
331 | 332 | $gallery = []; |
332 | 333 |
|
333 | 334 | $mediaGallery = $product->getMediaGalleryEntries(); |
| 335 | + /** @var ProductAttributeMediaGalleryEntryInterface $mediaEntry */ |
334 | 336 | foreach ($mediaGallery as $mediaEntry) { |
335 | 337 | if ($mediaEntry->isDisabled()) { |
336 | 338 | continue; |
337 | 339 | } |
338 | | - |
339 | | - if (in_array('image', $mediaEntry->getTypes(), true) || !$baseImage) { |
340 | | - $baseImage = $mediaEntry->getFile(); |
| 340 | + if (!$baseImage || $this->isMainImage($mediaEntry)) { |
| 341 | + $baseImage = $mediaEntry; |
341 | 342 | } |
342 | 343 |
|
343 | | - $gallery[$mediaEntry->getId()] = $this->getAllSizeImages($mediaEntry->getFile()); |
| 344 | + $gallery[$mediaEntry->getId()] = $this->collectImageData($mediaEntry); |
344 | 345 | } |
345 | 346 |
|
346 | 347 | if (!$baseImage) { |
347 | 348 | return []; |
348 | 349 | } |
349 | 350 |
|
350 | | - $resultGallery = $this->getAllSizeImages($baseImage); |
| 351 | + $resultGallery = $this->collectImageData($baseImage); |
351 | 352 | $resultGallery['gallery'] = $gallery; |
352 | 353 |
|
353 | 354 | return $resultGallery; |
354 | 355 | } |
355 | 356 |
|
| 357 | + /** |
| 358 | + * Checks if image is main image in gallery |
| 359 | + * |
| 360 | + * @param ProductAttributeMediaGalleryEntryInterface $mediaEntry |
| 361 | + * @return bool |
| 362 | + */ |
| 363 | + private function isMainImage(ProductAttributeMediaGalleryEntryInterface $mediaEntry): bool |
| 364 | + { |
| 365 | + return in_array('image', $mediaEntry->getTypes(), true); |
| 366 | + } |
| 367 | + |
| 368 | + /** |
| 369 | + * Returns image data for swatches |
| 370 | + * |
| 371 | + * @param ProductAttributeMediaGalleryEntryInterface $mediaEntry |
| 372 | + * @return array |
| 373 | + */ |
| 374 | + private function collectImageData(ProductAttributeMediaGalleryEntryInterface $mediaEntry): array |
| 375 | + { |
| 376 | + $image = $this->getAllSizeImages($mediaEntry->getFile()); |
| 377 | + $image[ProductAttributeMediaGalleryEntryInterface::POSITION] = $mediaEntry->getPosition(); |
| 378 | + $image['isMain'] =$this->isMainImage($mediaEntry); |
| 379 | + return $image; |
| 380 | + } |
| 381 | + |
356 | 382 | /** |
357 | 383 | * Get all size images |
358 | 384 | * |
|
0 commit comments