Skip to content

Commit

Permalink
Done: get category image
Browse files Browse the repository at this point in the history
  • Loading branch information
zamoroka committed May 7, 2020
1 parent da1606f commit 564ad9f
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 10 deletions.
60 changes: 60 additions & 0 deletions src/Plugin/CategoryImage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Zamoroka\ProductionImages\Plugin;

use Magento\Catalog\Model\Category;
use Magento\Catalog\Model\Category\FileInfo;
use Zamoroka\ProductionImages\Helper\Config;
use Zamoroka\ProductionImages\Model\ImageDownloader;

class CategoryImage
{
/** @var Config */
private $config;
/** @var ImageDownloader */
private $imageDownloader;

/**
* CategoryImage constructor.
*
* @param Config $config
* @param ImageDownloader $imageDownloader
*/
public function __construct(
Config $config,
ImageDownloader $imageDownloader
) {
$this->config = $config;
$this->imageDownloader = $imageDownloader;
}

/**
* @param Category $subject
* @param string $attributeCode
*
* @return string[]
*/
public function beforeGetImageUrl(Category $subject, $attributeCode = 'image')
{
if (!$this->config->isEnabled()) {
return [$attributeCode];
}
$image = $subject->getData($attributeCode);
if (!$image || !is_string($image)) {
return [$attributeCode];
}
$this->imageDownloader->downloadImage($this->getFilePath($image));

return [$attributeCode];
}

/**
* @param string $image
*
* @return string
*/
private function getFilePath(string $image)
{
return FileInfo::ENTITY_MEDIA_PATH . DIRECTORY_SEPARATOR . $image;
}
}
14 changes: 5 additions & 9 deletions src/Plugin/ProductImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,36 +27,32 @@ class ProductImage
/** @var MediaConfig */
private $mediaConfig;
/** @var PlaceholderFactory */
private $viewAssetPlaceholderFactory;
private $placeholderFactory;
/** @var Config */
private $config;
/** @var ImageDownloader */
private $imageDownloader;

/**
* @param ConfigInterface $presentationConfig
* @param PlaceholderFactory $viewAssetPlaceholderFactory
* @param PlaceholderFactory $placeholderFactory
* @param ParamsBuilder $imageParamsBuilder
* @param MediaConfig $mediaConfig
* @param Filesystem $filesystem
*
* @param Config $config
*
* @param ImageDownloader $imageDownloader
*
* @throws \Magento\Framework\Exception\FileSystemException
*/
public function __construct(
ConfigInterface $presentationConfig,
PlaceholderFactory $viewAssetPlaceholderFactory,
PlaceholderFactory $placeholderFactory,
ParamsBuilder $imageParamsBuilder,
MediaConfig $mediaConfig,
Filesystem $filesystem,
Config $config,
ImageDownloader $imageDownloader
) {
$this->presentationConfig = $presentationConfig;
$this->viewAssetPlaceholderFactory = $viewAssetPlaceholderFactory;
$this->placeholderFactory = $placeholderFactory;
$this->imageParamsBuilder = $imageParamsBuilder;
$this->filesystem = $filesystem;
$this->mediaConfig = $mediaConfig;
Expand Down Expand Up @@ -99,7 +95,7 @@ private function getFilePath(Product $product, string $imageId)
$imageMiscParams = $this->imageParamsBuilder->build($viewImageConfig);
$originalFilePath = $product->getData($imageMiscParams['image_type']);
if ($originalFilePath === null || $originalFilePath === 'no_selection') {
$imageAsset = $this->viewAssetPlaceholderFactory->create(['type' => $imageMiscParams['image_type']]);
$imageAsset = $this->placeholderFactory->create(['type' => $imageMiscParams['image_type']]);
$filePath = $this->mediaConfig->getMediaPath(
$imageAsset->getModule() . DIRECTORY_SEPARATOR . $imageAsset->getFilePath()
);
Expand Down
5 changes: 4 additions & 1 deletion src/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Catalog\Block\Product\ImageFactory">
<plugin name="getCheckProductImage" type="Zamoroka\ProductionImages\Plugin\ProductImage"/>
<plugin name="downloadProductImage" type="Zamoroka\ProductionImages\Plugin\ProductImage"/>
</type>
<type name="Magento\Catalog\Model\Category">
<plugin name="downloadCategoryImage" type="Zamoroka\ProductionImages\Plugin\CategoryImage"/>
</type>
</config>

0 comments on commit 564ad9f

Please sign in to comment.