Skip to content

Commit fef452a

Browse files
committed
MC-15440: Product Page Gallery Load Optimization
1 parent 5ca9e13 commit fef452a

File tree

4 files changed

+28
-24
lines changed

4 files changed

+28
-24
lines changed

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

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,8 @@ public function isMainImage($image)
207207
*/
208208
public function getImageAttribute($imageId, $attributeName, $default = null)
209209
{
210-
$attributes =
211-
$this->getConfigView()->getMediaAttributes('Magento_Catalog', Image::MEDIA_TYPE_CONFIG_NODE, $imageId);
210+
$attributes = $this->getConfigView()
211+
->getMediaAttributes('Magento_Catalog', Image::MEDIA_TYPE_CONFIG_NODE, $imageId);
212212
return $attributes[$attributeName] ?? $default;
213213
}
214214

@@ -239,23 +239,4 @@ private function getGalleryImagesConfig()
239239

240240
return $this->getData('gallery_images_config');
241241
}
242-
243-
/**
244-
* Get main product image
245-
*
246-
* @param string $size
247-
* @return string
248-
*/
249-
public function getMainProductImage($size = 'medium_image_url')
250-
{
251-
foreach ($this->getGalleryImages() as $image) {
252-
$image = $image->getData($size);
253-
254-
if (!$image) {
255-
return $this->_imageHelper->getDefaultPlaceholderUrl('image');
256-
}
257-
258-
return $image;
259-
}
260-
}
261242
}

app/code/Magento/Catalog/Helper/Image.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace Magento\Catalog\Helper;
77

88
use Magento\Framework\App\Helper\AbstractHelper;
9+
use Magento\Framework\View\Element\Block\ArgumentInterface;
910

1011
/**
1112
* Catalog image helper
@@ -14,7 +15,7 @@
1415
* @SuppressWarnings(PHPMD.TooManyFields)
1516
* @since 100.0.2
1617
*/
17-
class Image extends AbstractHelper
18+
class Image extends AbstractHelper implements ArgumentInterface
1819
{
1920
/**
2021
* Media config node

app/code/Magento/Catalog/view/frontend/layout/catalog_product_view.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,11 @@
121121
</arguments>
122122
</block>
123123
</container>
124-
<block class="Magento\Catalog\Block\Product\View\Gallery" name="product.info.media.image" template="Magento_Catalog::product/view/gallery.phtml"/>
124+
<block class="Magento\Catalog\Block\Product\View\Gallery" name="product.info.media.image" template="Magento_Catalog::product/view/gallery.phtml">
125+
<arguments>
126+
<argument name="imageHelper" xsi:type="object">Magento\Catalog\Helper\Image</argument>
127+
</arguments>
128+
</block>
125129
<container name="skip_gallery_after.wrapper" htmlTag="div" htmlClass="action-skip-wrapper">
126130
<block class="Magento\Framework\View\Element\Template" after="product.info.media.image" name="skip_gallery_after" template="Magento_Theme::html/skip.phtml">
127131
<arguments>

app/code/Magento/Catalog/view/frontend/templates/product/view/gallery.phtml

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,28 @@
1313
*/
1414
?>
1515

16+
<?php
17+
$images = $block->getGalleryImages()->getItems();
18+
$mainImage = current(array_filter($images, function ($img) use ($block) {
19+
return $block->isMainImage($img);
20+
}));
21+
22+
if (!empty($images) && empty($mainImage)) {
23+
$mainImage = $block->getGalleryImages()->getFirstItem();
24+
}
25+
26+
$helper = $block->getData('imageHelper');
27+
$mainImageData = $mainImage ?
28+
$mainImage->getData('medium_image_url') :
29+
$helper->getDefaultPlaceholderUrl('image');
30+
31+
?>
32+
1633
<div class="gallery-placeholder _block-content-loading" data-gallery-role="gallery-placeholder">
1734
<img
35+
alt="main product photo"
1836
class="gallery-placeholder__image"
19-
src="<?= /* @escapeNotVerified */ $block->getMainProductImage() ?>"
37+
src="<?= /* @noEscape */ $mainImageData ?>"
2038
/>
2139
</div>
2240

0 commit comments

Comments
 (0)