I'm trying to remove the video from the gallery in product page and I saw that the method getGalleryImages()
in the Magento\Catalog\Block\Product\View\Gallery
block gets all types of media (image and external-video).
So I have created my custom module where I put a block that extends Magento\Catalog\Block\Product\View\Gallery
and I override getGalleryImages()
method.
There wasn't any problem to get only the images, but now in my gallery I can't see the thumbnail, I get only the main gallery.
Here my code, my custom block: <Vendor>\<Module>\Block\MediaGallery
:
<?php namespace <Vendor>\<Module>\Block; use Magento\Framework\Json\EncoderInterface; class MediaGallery extends \Magento\Catalog\Block\Product\View\Gallery {
public function __construct(
\Magento\Catalog\Block\Product\Context $context,
\Magento\Framework\Stdlib\ArrayUtils $arrayUtils,
EncoderInterface $jsonEncoder,
array $data = []
)
{
parent::__construct(
$context,
$arrayUtils,
$jsonEncoder,
$data
);
}
public function getGalleryImages()
{
$product = $this->getProduct();
$images = $product->getMediaGalleryImages();
if ($images instanceof \Magento\Framework\Data\Collection) {
foreach ($images as $key => $image) {
/* @var \Magento\Framework\DataObject $image */
if ($image->getMediaType() == 'image') {
$image->setData(
'small_image_url',
$this->_imageHelper->init($product, 'product_page_image_small')
->setImageFile($image->getFile())
->getUrl()
);
$image->setData(
'medium_image_url',
$this->_imageHelper->init($product, 'product_page_image_medium')
->constrainOnly(true)->keepAspectRatio(true)->keepFrame(false)
->setImageFile($image->getFile())
->getUrl()
);
$image->setData(
'large_image_url',
$this->_imageHelper->init($product, 'product_page_image_large')
->constrainOnly(true)->keepAspectRatio(true)->keepFrame(false)
->setImageFile($image->getFile())
->getUrl()
);
} else {
$images->removeItemByKey($key);
}
}
}
return $images;
}}
That's is what I get with my custom block:
Setting in this way the block in layout:
<container name="product.info.media" after="product.info.main">
<block class="<Vendor>\<Module>\Block\MediaGallery" name="product.info.media.image" template="Magento_Catalog::product/view/gallery.phtml"/>
</container>
While with this configuration:
<container name="product.info.media" after="product.info.main">
<block class="Magento\Catalog\Block\Product\View\Gallery" name="product.info.media.image" template="product/view/gallery.phtml"/>
</container>
I don't post the phtml file, because is the same of the default module, I only copied it in my theme folder in the same position.