0

We have problems with images in frontend side.

PHP Fatal error: Uncaught TypeError: Argument 1 passed to Magento\Catalog\Block\Product\ImageFactory::create() must be an instance of Magento\Catalog\Model\Product, array given, called in ...

If change "return $this->imageFactory->create($data);" in this method for "return false;" - the error disappears. But we have a new problem in different sections:

PHP Fatal error: Uncaught Error: Call to a member function toHtml() on boolean in ...

This error after getImage(...)->toHtml and getImageUrl(...)->toHtml

I debbug params, what get methods getImage and getImageUrl. Emptiness gets there. All sections where there is a withdrawal of goods do not work on the site. The images are displayed correctly in the admin panel, but in frontend, I think the problem is in the pictures.

What can help for solve this problems? (Magento 2.2.5, php 7.1, mysql 5.7)

1 Answer 1

0

.phtml file

<a href="<?= /* @escapeNotVerified */ $block->getProductUrl($_item) ?>" class="product photo product-item-photo">
            <?= $this->getImage($_item, $image)->toHtml() ?>
</a>

getImage() Function in Block File

 <?php
namespace Vendor\Module\Block;

class file_name extends Template
{
    protected $imageBuilder;

    public function __construct(
        \Magento\Catalog\Block\Product\Context $contextImage,
        array $data = []
    )
    {
        $this->imageBuilder = $contextImage->getImageBuilder();
        parent::__construct($context, $data);
    }
    public function getImage($product, $imageId, $attributes = [])
    {
        return $this->imageBuilder->setProduct($product)
            ->setImageId($imageId)
            ->setAttributes($attributes)
            ->create();
    }
}
2
  • Thank you for your answer, but we have a problem in all templates where catalog products are displayed. You definitely don't need to edit the code. Everything works correctly on the main site. We have difficulties deploying a test copy for development.
    – WhoYou
    Jan 10, 2022 at 12:25
  • In case if the image is not showing on all pages the possible cause your Static Contents are most likely out of date. You can regenerate your static contents with this command on your server from your Magento 2 root folder. Note that you may or may not need to delete files from pub/static/frontend php bin/magento setup:static-content:deploy <locale> Jan 13, 2022 at 2:11

Your Answer

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

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