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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<description>Assert category grid page basic columns values for default category</description>
</annotations>

<seeElement selector="{{AdminMediaGalleryCatalogUiCategoryGridSection.image('1','image')}}" stepKey="assertImageColumn"/>
<seeElement selector="{{AdminMediaGalleryCatalogUiCategoryGridSection.path('1')}}" stepKey="assertPathColumn"/>
<seeElement selector="{{AdminMediaGalleryCatalogUiCategoryGridSection.name('1', 'Default Category')}}" stepKey="assertNameColumn"/>
<seeElement selector="{{AdminMediaGalleryCatalogUiCategoryGridSection.displayMode('1', 'PRODUCTS')}}" stepKey="assertDisplayModeColumn"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<sections xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Page/etc/SectionObject.xsd">
<section name="AdminMediaGalleryCatalogUiCategoryGridSection">
<element name="image" type="text" selector="//tr[{{row}}]//td[count(//div[@data-role='grid-wrapper']//tr//th[contains(., 'Image')]/preceding-sibling::th) +1]//img[contains(@src, '{{imageName}}')]" parameterized="true"/>
<element name="path" type="text" selector="//tr[{{row}}]//td[count(//div[@data-role='grid-wrapper']//tr//th[contains(., 'Path')]/preceding-sibling::th)]" parameterized="true"/>
<element name="name" type="text" selector="//tr[{{row}}]//td[count(//div[@data-role='grid-wrapper']//tr//th[contains(., 'Name')]/preceding-sibling::th) +1 ]//*[text()='{{categoryName}}']" parameterized="true"/>
<element name="displayMode" type="text" selector="//tr[{{row}}]//td[count(//div[@data-role='grid-wrapper']//tr//th[contains(., 'Display Mode')]/preceding-sibling::th) +1 ]//*[text()='{{productsText}}']" parameterized="true"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
*/
namespace Magento\MediaGalleryCatalogUi\Ui\Component\Listing\Columns;

use Magento\Catalog\Helper\Image;
use Magento\Framework\DataObject;
use Magento\Catalog\Model\Category\Image;
use Magento\Catalog\Model\CategoryRepository;
use Magento\Framework\View\Asset\Repository as AssetRepository;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Store\Model\Store;
Expand All @@ -27,48 +28,87 @@ class Thumbnail extends Column
/**
* @var Image
*/
private $imageHelper;
private $categoryImage;

/**
* @var CategoryRepository
*/
private $categoryRepository;

/**
* @var AssetRepository
*/
private $assetRepository;

/**
* @var string[]
*/
private $defaultPlaceholder;

/**
* Thumbnail constructor.
* @param ContextInterface $context
* @param UiComponentFactory $uiComponentFactory
* @param StoreManagerInterface $storeManager
* @param Image $image
* @param Image $categoryImage
* @param CategoryRepository $categoryRepository
* @param AssetRepository $assetRepository
* @param array $defaultPlaceholder
* @param array $components
* @param array $data
*/
public function __construct(
ContextInterface $context,
UiComponentFactory $uiComponentFactory,
StoreManagerInterface $storeManager,
Image $image,
Image $categoryImage,
CategoryRepository $categoryRepository,
AssetRepository $assetRepository,
array $defaultPlaceholder = [],
array $components = [],
array $data = []
) {
parent::__construct($context, $uiComponentFactory, $components, $data);
$this->imageHelper = $image;
$this->storeManager = $storeManager;
$this->categoryImage = $categoryImage;
$this->categoryRepository = $categoryRepository;
$this->assetRepository = $assetRepository;
$this->defaultPlaceholder = $defaultPlaceholder;
}

/**
* Prepare Data Source
*
* @param array $dataSource
* @return array
* @throws \Magento\Framework\Exception\LocalizedException
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function prepareDataSource(array $dataSource)
{
if (isset($dataSource['data']['items'])) {
$fieldName = $this->getData('name');
foreach ($dataSource['data']['items'] as & $item) {
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!

$imageHelper = $this->imageHelper->init($category, 'product_listing_thumbnail');
$item[$fieldName . '_src'] = $imageHelper->getUrl();
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->categoryImage->getUrl(
$this->categoryRepository->get($item['entity_id'])
);

if (!empty($src)) {
$item[$fieldName . '_src'] = $src;
continue;
}
}

$item[$fieldName . '_src'] = $this->assetRepository->getUrl($this->defaultPlaceholder['image']);
}

return $dataSource;
Expand Down
7 changes: 7 additions & 0 deletions app/code/Magento/MediaGalleryCatalogUi/etc/adminhtml/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,11 @@
</argument>
</arguments>
</type>
<type name="Magento\MediaGalleryCatalogUi\Ui\Component\Listing\Columns\Thumbnail">
<arguments>
<argument name="defaultPlaceholder" xsi:type="array">
<item name="image" xsi:type="string">Magento_MediaGalleryCatalogUi::images/category/placeholder/image.jpg</item>
</argument>
</arguments>
</type>
</config>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.