cancel
Showing results for 
Search instead for 
Did you mean: 

ImageBuilder block override

SOLVED

ImageBuilder block override

Hello,

using 2.2.1 here. I need to override module-catalog/Block/Product/ImageBuilder.php block, so I did define my own:

<?php
namespace Hoop\Util\Block\Product;
use Mageno\Catalog\Helper\ImageFactory as HelperFactory;

class ImageBuilder {..}

but when I call it I get:

4 exception(s):
Exception #0 (Magento\Framework\Exception\LocalizedException): Invalid block type: Magento\Catalog\Block\Product\ListProduct
Exception #1 (ReflectionException): Class Hoop\Util\Block\Product\ImageFactory does not exist
Exception #2 (ReflectionException): Class Hoop\Util\Block\Product\ImageFactory does not exist
Exception #3 (RuntimeException): Source class "\Hoop\Util\Block\Product\Image" for "Hoop\Util\Block\Product\ImageFactory" generation does not exist.

ofc I want to use magento's ImageFactory, not redefine it.

what am I doing wrong?

Thanks a lot

1 ACCEPTED SOLUTION

Accepted Solutions

Re: ImageBuilder block override

ok I found the solution myself:

<?php
class ImageBuilder extends \Magento\Catalog\Block\Product\ImageBuilder
{

    public function create()
    {
        /** @var \Magento\Catalog\Helper\Image $helper */
        $helper = $this->helperFactory->create()
            ->init($this->product, $this->imageId);

        $template = $helper->getFrame()
            ? 'Magento_Catalog::product/image.phtml'
            : 'Magento_Catalog::product/image_with_borders.phtml';

        $imagesize = $helper->getResizedImageInfo();

        $data = [
            'data' => [
                'template' => $template,
                'image_url' => $this->product->load($this->product->getId())->getMyCustomAttributeImage(),
                'width' => $helper->getWidth(),
                'height' => $helper->getHeight(),
                'label' => $helper->getLabel(),
                'ratio' =>  $this->getRatio($helper),
                'custom_attributes' => $this->getCustomAttributes(),
                'resized_image_width' => !empty($imagesize[0]) ? $imagesize[0] : $helper->getWidth(),
                'resized_image_height' => !empty($imagesize[1]) ? $imagesize[1] : $helper->getHeight(),
            ],
        ];

        return $this->imageFactory->create($data);
    }
}

View solution in original post

5 REPLIES 5

Re: ImageBuilder block override

I see the problem must be in the constructor:

public function __construct(
        HelperFactory $helperFactory,
        ImageFactory $imageFactory
    ) {
        //$this->helperFactory = $helperFactory;
        $this->imageFactory = $imageFactory;
    }

which I don't understand in the original magento block either, because in fact I can't find any ImageFactory inside Helper folder.

Any hint?

thanks

Re: ImageBuilder block override

nobody on this topic? I really need help with this.

 

Thanks

Re: ImageBuilder block override

What do you want to do for ImageBuilder Class? Are you want to override or what need to do and please give a full details for your queru.

If Issue Solved, Click Kudos/Accept As solutions. Get Magento insight from
Magento 2 Blogs/Tutorial

Re: ImageBuilder block override

Hello Rakesh,

thanks for your answer. I need to override the create function, in order to fill the $data array with my own info, in particular the 'image_url' parameter will be filled with my own custom attribute.

 

thanks a lot

Re: ImageBuilder block override

ok I found the solution myself:

<?php
class ImageBuilder extends \Magento\Catalog\Block\Product\ImageBuilder
{

    public function create()
    {
        /** @var \Magento\Catalog\Helper\Image $helper */
        $helper = $this->helperFactory->create()
            ->init($this->product, $this->imageId);

        $template = $helper->getFrame()
            ? 'Magento_Catalog::product/image.phtml'
            : 'Magento_Catalog::product/image_with_borders.phtml';

        $imagesize = $helper->getResizedImageInfo();

        $data = [
            'data' => [
                'template' => $template,
                'image_url' => $this->product->load($this->product->getId())->getMyCustomAttributeImage(),
                'width' => $helper->getWidth(),
                'height' => $helper->getHeight(),
                'label' => $helper->getLabel(),
                'ratio' =>  $this->getRatio($helper),
                'custom_attributes' => $this->getCustomAttributes(),
                'resized_image_width' => !empty($imagesize[0]) ? $imagesize[0] : $helper->getWidth(),
                'resized_image_height' => !empty($imagesize[1]) ? $imagesize[1] : $helper->getHeight(),
            ],
        ];

        return $this->imageFactory->create($data);
    }
}