Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1711: Use product model instead of data object for catalog image helper init #29435

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace Magento\MediaGalleryCatalogUi\Ui\Component\Listing\Columns;

use Magento\Catalog\Helper\Image;
use Magento\Framework\DataObject;
use Magento\Catalog\Model\ProductFactory;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Store\Model\Store;
Expand All @@ -29,11 +29,17 @@ class Thumbnail extends Column
*/
private $imageHelper;

/**
* @var ProductFactory
*/
private $productFactory;

/**
* @param ContextInterface $context
* @param UiComponentFactory $uiComponentFactory
* @param StoreManagerInterface $storeManager
* @param Image $image
* @param ProductFactory $productFactory
* @param array $components
* @param array $data
*/
Expand All @@ -42,12 +48,14 @@ public function __construct(
UiComponentFactory $uiComponentFactory,
StoreManagerInterface $storeManager,
Image $image,
ProductFactory $productFactory,
array $components = [],
array $data = []
) {
parent::__construct($context, $uiComponentFactory, $components, $data);
$this->imageHelper = $image;
$this->storeManager = $storeManager;
$this->productFactory = $productFactory;
}

/**
Expand All @@ -64,7 +72,7 @@ public function prepareDataSource(array $dataSource)
if (isset($item[$fieldName])) {
$item[$fieldName . '_src'] = $this->getUrl($item[$fieldName]);
} else {
$category = new DataObject($item);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, @jmonteros422 , after a closer look the approach appears to be completely invalid: this class is responsible to prepare the image for category, not for the product, so CategoryFactory and Category\ImageFactory should be used. Also we have to find out how to get a placeholder image in case category does not have one assigned.

Here is how the method should look like based on my understanding:

    public function prepareDataSource(array $dataSource)
    {
        if (!isset($dataSource['data']['items'])) {
            return $dataSource;
        }

        $fieldName = $this->getData('name');
        foreach ($dataSource['data']['items'] as & $item) {
            if (isset($item[$fieldName])) {
                $item[$fieldName . '_src'] = $this->getUrl($item[$fieldName]);
                continue;
            }

            if (isset($item['entity_id'])) {
                $src = $this->imageFactory->create()->getUrl(
                    $this->categoryRepository->get($item['entity_id'])
                );
                $item[$fieldName . '_src'] = $src;
                continue;
            }

            $item[$fieldName . '_src'] = $defaultImage;
        }
    }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok Sergii, will check that one out. Thank you!

$category = $this->productFactory->create($item);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be:

Suggested change
$category = $this->productFactory->create($item);
$category = $this->productFactory->create(['data' => $item]);

Can you please check

$imageHelper = $this->imageHelper->init($category, 'product_listing_thumbnail');
$item[$fieldName . '_src'] = $imageHelper->getUrl();
}
Expand Down