From 564ad9fe38d214c13d60435e49faa868c766ebca Mon Sep 17 00:00:00 2001 From: zamoroka Date: Thu, 7 May 2020 21:29:13 +0300 Subject: [PATCH] Done: get category image --- src/Plugin/CategoryImage.php | 60 ++++++++++++++++++++++++++++++++++++ src/Plugin/ProductImage.php | 14 +++------ src/etc/di.xml | 5 ++- 3 files changed, 69 insertions(+), 10 deletions(-) create mode 100644 src/Plugin/CategoryImage.php diff --git a/src/Plugin/CategoryImage.php b/src/Plugin/CategoryImage.php new file mode 100644 index 0000000..95bcd0c --- /dev/null +++ b/src/Plugin/CategoryImage.php @@ -0,0 +1,60 @@ +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; + } +} diff --git a/src/Plugin/ProductImage.php b/src/Plugin/ProductImage.php index 6bf6daa..6614a2d 100644 --- a/src/Plugin/ProductImage.php +++ b/src/Plugin/ProductImage.php @@ -27,7 +27,7 @@ class ProductImage /** @var MediaConfig */ private $mediaConfig; /** @var PlaceholderFactory */ - private $viewAssetPlaceholderFactory; + private $placeholderFactory; /** @var Config */ private $config; /** @var ImageDownloader */ @@ -35,20 +35,16 @@ class ProductImage /** * @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, @@ -56,7 +52,7 @@ public function __construct( ImageDownloader $imageDownloader ) { $this->presentationConfig = $presentationConfig; - $this->viewAssetPlaceholderFactory = $viewAssetPlaceholderFactory; + $this->placeholderFactory = $placeholderFactory; $this->imageParamsBuilder = $imageParamsBuilder; $this->filesystem = $filesystem; $this->mediaConfig = $mediaConfig; @@ -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() ); diff --git a/src/etc/di.xml b/src/etc/di.xml index 670ebbb..56fbdf1 100644 --- a/src/etc/di.xml +++ b/src/etc/di.xml @@ -2,6 +2,9 @@ - + + + +