1

I am trying to override the way that the Images in the Catalog, Checkout and Cart are going to be loaded someway different then Magento does it by default.

Thats why I'm trying to create a module that changes ImageFactory.php for me.

Module Structure:

enter image description here

I have created an appropriate di.xml:

<?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\ImageFactory" type="Vendor\Name\Block\Product\ImageFactory" />
</config>

In ImageFactory I have changed the module that's being loaded by Magento2, which is located correctly in the structure.

However, neither the Phtml or the ImageFactory is being called by Magento. The module is actively read by Magento as I see the adminhtml settings that I've configured in config.xml and such.

2
  • Show please code the layout and class Block Commented Dec 23, 2018 at 21:23
  • Have you found any solution? Commented Apr 9, 2021 at 9:24

1 Answer 1

0

I ran into this as well and found out that in ImageBuilder this ImageFactory class is not included in the way it should be included.

Usually one should include a class like this:

use Magento\Catalog\Helper\ImageFactory;

However in one of the most important classes where it is used, it is included in the construct without the full path. In Magento\Catalog\Block\Product\ImageBuilder.php it is included in the construct without the full path:

use Magento\Catalog\Helper\ImageFactory as HelperFactory;
use Magento\Catalog\Model\Product;
...
public function __construct(
    HelperFactory $helperFactory,
    ImageFactory $imageFactory
) {
    $this->helperFactory = $helperFactory;
    $this->imageFactory = $imageFactory;
}

So in the above it has no full path to the imageFactory so it searches it locally (and low and behold it is in the same directory). This is bad practice and means that you can override it all you want, if you do not override ImageBuilder as well, nothing will change for the major part of where it is used.

I ended up making a preference for both and including the full path in the ImageBuilder construct for ImageFactory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.