I'm trying to change the image gallery so that it doesn't display the product name as a default image caption. After tracing through the code I found the getGalleryImagesJson method of the Magento\Catalog\Block\Product\View\Gallery class that fit the bill. I added my own class, extended the core one, set my preference line in di.xml and added the <sequence> node in my module.xml. My class is being utilized and no errors are thrown. However, even if I blank out my entire class so I'm not modifying any functionality at all, the thumbnail scroller at the bottom of the fotorama widget isn't there when my class is in use. If I comment out the <preference> line in my di.xml the thumbnails come back. This makes no sense at all and I'm stumped. Any ideas?


<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <!-- Override the Product Image Gallery to remove the default caption -->
    <preference for="Magento\Catalog\Block\Product\View\Gallery" type="Mymodule\Catalog\Block\Rewrite\Product\View\CWGallery" />


<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Mymodule_Catalog" setup_version="1.0.0">
            <module name="Magento_Catalog" />


Note: This is the final version of my rewrite. Originally I had only overwritten the getGalleryImagesJson method. When that didn't work properly I began adding more things, including the constructor and any usage of getConfigView

namespace Mymodule\Catalog\Block\Rewrite\Product\View;

use Magento\Catalog\Helper\Image;
use Magento\Framework\Data\Collection;
use Magento\Framework\Json\EncoderInterface;
use Magento\Catalog\Block\Product\View\Gallery;

class CWGallery extends 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 getGalleryImagesJson()
        $images = json_decode(parent::getGalleryImagesJson(), true);

        $i = 0;

        foreach ($this->getGalleryImages() as $image)
            if (!$image->getLabel())
                $images[$i]['caption'] = "";

        return json_encode($images);

    public function getImageAttribute($imageId, $attributeName, $default = null)
        $attributes =
            $this->getConfigView()->getMediaAttributes('Magento_Catalog', Image::MEDIA_TYPE_CONFIG_NODE, $imageId);
        return isset($attributes[$attributeName]) ? $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;


After struggling with this for hours I learned about the Plugin functionality in Magento 2 and tried that (I'm new to M2 so I don't know all the fancy new tools). This works! I did an after plugin for that same method and it successfully massages the caption without breaking the whole gallery widget.

But I would still like to understand why my original method didn't work. It seems like everything was correct, no errors, and my code was being executed. So why did it break things down the line?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.