Magento – Magento 2 – Remove video from gallery in product page

magento2PHPproduct

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: enter image description here

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 get what I want to see: enter image description here

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.

Best Answer

I'm not exactly sure what you're trying to do. Do you want to remove a video from the product? Or only not show it?

Here is my code to remove all product images, you can put a condition in there to remove only the video you want:

/** @var \Magento\Catalog\Model\Product $product */

$gallery = $product->getMediaGalleryEntries();
foreach ($gallery as $key => $entry) {
    $image = $entry->getFile();
    if (your condition) {
        $image = 'pub/media/catalog/product' . $image;
        // Remove entry from Media Gallery
        unset($gallery[$key]);
        // Delete file
        unlink($image);
    }
}
// Save changed media gallery to product     
$product->setMediaGalleryEntries($gallery);
// Save Product
$product = $this->productRepository->save($product);

I hope this helps.

Related Topic