I am trying to extend a Magento2 class Magento\Catalog\Block\Product\View\Gallery
. This is because I want to add videos to products and that only works if I add a line of code in an array. Because this is a Magento class it gets overwritten at every update so I am trying to extend/overwrite it but for some reason, it does not want to work. I don't see the module if I run
bin/magento module:status
di.xml app/code/Headline/ProductVideo/etc
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Catalog\Block\Product\View\Gallery"
type="Headline\ProductVideo\Block\Product\View\Gallery"></preference>
</config>
module.xml app/code/Headline/ProductVideo/etc
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Headline_ProductVideo" setup_version="1.0.1">
<sequence>
<module name="Magento_Catalog"/>
</sequence>
</module>
</config>
registration.php app/code/Headline/ProductVideo
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Headline_ProductVideo',
__DIR__
);
Gallery.php app/code/Headline/ProductVideo/Block/Product/View
<?php
namespace Headline\ProductVideo\Block\Product\View;
use Magento\Framework\Data\Collection;
use Magento\Framework\Json\EncoderInterface;
use Magento\Catalog\Helper\Image;
class Gallery extends \Magento\Catalog\Block\Product\View\Gallery
{
/**
* @param \Magento\Catalog\Block\Product\Context $context
* @param \Magento\Framework\Stdlib\ArrayUtils $arrayUtils
* @param EncoderInterface $jsonEncoder
* @param array $data
*/
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 $image) {
/* @var \Magento\Framework\DataObject $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_no_frame')
->setImageFile($image->getFile())
->getUrl()
);
$image->setData(
'large_image_url',
$this->_imageHelper->init($product, 'product_page_image_large_no_frame')
->setImageFile($image->getFile())
->getUrl()
);
}
}
return $images;
}
/**
* Return magnifier options
*
* @return string
*/
public function getMagnifier()
{
return $this->jsonEncoder->encode($this->getVar('magnifier'));
}
/**
* Return breakpoints options
*
* @return string
*/
public function getBreakpoints()
{
return $this->jsonEncoder->encode($this->getVar('breakpoints'));
}
/**
* Retrieve product images in JSON format
*
* @return string
*/
public function getGalleryImagesJson()
{
die();
$imagesItems = [];
foreach ($this->getGalleryImages() as $image) {
$imagesItems[] = [
'thumb' => $image->getData('small_image_url'),
'img' => $image->getData('medium_image_url'),
'full' => $image->getData('large_image_url'),
'caption' => ($image->getLabel() ?: $this->getProduct()->getName()),
'position' => $image->getPosition(),
'isMain' => $this->isMainImage($image),
'type' => str_replace('external-', '', $image->getMediaType()),
'videoUrl' => $image->getVideoUrl(),
'video' => $image->getVideoUrl(), //video fix
];
}
if (empty($imagesItems)) {
$imagesItems[] = [
'thumb' => $this->_imageHelper->getDefaultPlaceholderUrl('thumbnail'),
'img' => $this->_imageHelper->getDefaultPlaceholderUrl('image'),
'full' => $this->_imageHelper->getDefaultPlaceholderUrl('image'),
'caption' => '',
'position' => '0',
'isMain' => true,
'type' => 'image',
'videoUrl' => null,
];
}
return json_encode($imagesItems);
}
/**
* Retrieve gallery url
*
* @param null|\Magento\Framework\DataObject $image
* @return string
*/
public function getGalleryUrl($image = null)
{
$params = ['id' => $this->getProduct()->getId()];
if ($image) {
$params['image'] = $image->getValueId();
}
return $this->getUrl('catalog/product/gallery', $params);
}
/**
* Is product main image
*
* @param \Magento\Framework\DataObject $image
* @return bool
*/
public function isMainImage($image)
{
$product = $this->getProduct();
return $product->getImage() == $image->getFile();
}
/**
* @param string $imageId
* @param string $attributeName
* @param string $default
* @return string
*/
public function getImageAttribute($imageId, $attributeName, $default = null)
{
$attributes =
$this->getConfigView()->getMediaAttributes('Magento_Catalog', Image::MEDIA_TYPE_CONFIG_NODE, $imageId);
return $attributes[$attributeName] ?? $default;
}
/**
* Retrieve config view
*
* @return \Magento\Framework\Config\View
*/
private function getConfigView()
{
if (!$this->configView) {
$this->configView = $this->_viewConfig->getViewConfig();
}
return $this->configView;
}
}
In Headline\ProductVideo\Block\Product\View\Gallery
I have the same code as the original file but added the line of code in the array. In the original block classes, I changed the path to the Gallery.php file. I changed that in catalog_product_view_config.xml
and catalog_product_view_2-cols