Skip to content

Commit 7e7cb13

Browse files
committed
Add Category Image Url Converter
1 parent fa34b8f commit 7e7cb13

File tree

7 files changed

+146
-2
lines changed

7 files changed

+146
-2
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
33
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
44

5+
#### [2.0.1] - 2021-03-08
6+
7+
##### Added
8+
- Add Category Media Url Support
9+
510
#### [1.0.0] - 2020-03-03
611

712
##### Added
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<?php declare(strict_types=1);
2+
/**
3+
* This file is part of the Magebit_StaticContentProcessor package
4+
*
5+
* DISCLAIMER
6+
*
7+
* Do not edit or add to this file if you wish to upgrade Magebit_StaticContentProcessor
8+
* to newer versions in the future.
9+
*
10+
* @copyright Copyright (c) 2021 Magebit (https://magebit.com/)
11+
* @author Liene Tunne <[email protected]>
12+
* @license GNU General Public License ("GPL") v3.0
13+
*
14+
* For the full copyright and license information, please view the LICENSE
15+
* file that was distributed with this source code.
16+
*/
17+
18+
namespace Magebit\StaticContentProcessor\Model\Indexer\DataProvider\Category;
19+
20+
use Magento\Framework\UrlInterface;
21+
use Magento\Store\Model\ScopeInterface;
22+
use Magento\Framework\App\Config\ScopeConfigInterface;
23+
use Divante\VsbridgeIndexerCore\Api\DataProviderInterface;
24+
use Magento\Catalog\Model\Config;
25+
use Magento\Framework\Exception\LocalizedException;
26+
27+
/**
28+
* Class WysiwygBlockData
29+
*/
30+
class ImageUrlRewrite implements DataProviderInterface
31+
{
32+
/**
33+
* Entity code
34+
*/
35+
const ENTITY = 'catalog_category';
36+
37+
/**
38+
* @var ScopeConfigInterface
39+
*/
40+
private $scopeConfig;
41+
42+
/**
43+
* @var Config
44+
*/
45+
private $catalogConfig;
46+
47+
48+
/**
49+
* ImageUrlRewrite constructor.
50+
* @param ScopeConfigInterface $scopeConfig
51+
* @param Config $catalogConfig
52+
*/
53+
public function __construct(
54+
ScopeConfigInterface $scopeConfig,
55+
Config $catalogConfig
56+
) {
57+
$this->scopeConfig = $scopeConfig;
58+
$this->catalogConfig = $catalogConfig;
59+
}
60+
61+
/**
62+
* @param array $indexData
63+
* @param int $storeId
64+
* @return array
65+
* @throws LocalizedException
66+
*/
67+
public function addData(array $indexData, $storeId): array
68+
{
69+
$processable = [];
70+
71+
$rewriteCatMediaUrl = $this->scopeConfig->getValue(
72+
'vsbridge_indexer_settings/url_rewrites/parse_category_media',
73+
ScopeInterface::SCOPE_STORE
74+
);
75+
76+
if ($rewriteCatMediaUrl) {
77+
foreach ($indexData as $categoryId => $categoryData) {
78+
foreach ($categoryData as $attributeCode => $attribute) {
79+
80+
if ($this->checkAttributeIsImage($attributeCode)) {
81+
$processable[] = [
82+
'categoryId' => $categoryId,
83+
'attributeId' => $attributeCode,
84+
'content' => $attribute
85+
];
86+
}
87+
}
88+
}
89+
90+
$filteredIndexData = $this->convertMediaUrl($processable);
91+
92+
foreach ($filteredIndexData as $data) {
93+
$indexData[$data['categoryId']][$data['attributeId']] = $data['content'];
94+
}
95+
}
96+
97+
return $indexData;
98+
}
99+
100+
/**
101+
* @param string|null $attributeCode
102+
* @return bool
103+
* @throws LocalizedException
104+
*/
105+
protected function checkAttributeIsImage(?string $attributeCode): bool
106+
{
107+
$attributeData = $this->catalogConfig->getAttribute(self::ENTITY, $attributeCode);
108+
109+
return $attributeData->getFrontendInput() == 'image' ? true : false;
110+
}
111+
112+
/**
113+
* @param array $data
114+
* @return array
115+
*/
116+
protected function convertMediaUrl(array $data): array
117+
{
118+
$urlRewritesMedia = $this->scopeConfig->getValue(
119+
'vsbridge_indexer_settings/url_rewrites/media_url',
120+
ScopeInterface::SCOPE_STORE
121+
);
122+
123+
foreach ($data as &$attribute) {
124+
// Remove store Url
125+
$cleanVSFMediaPath = substr($urlRewritesMedia, strpos($urlRewritesMedia, '/media'));
126+
$attribute['content'] = str_replace('/media', $cleanVSFMediaPath, $attribute['content']);
127+
}
128+
129+
return $data;
130+
}
131+
}

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ You can find these configuration fields in:
2828
You can also specify which category and product attributes to run through the processor. As an example, you
2929
could select product `description` attribute and all the links and images will be converted with VSF urls.
3030

31+
You can also enable Category Image attribute url processor. It will convert all category attributes as image to VSF urls.
32+
3133
#### As a dependency
3234

3335
You can also use this module as a dependency for your own module:

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "Converts magento URL's to VSF during indexation",
44
"license": "GPL-3.0-only",
55
"type": "magento2-component",
6-
"version": "2.0.0",
6+
"version": "2.0.1",
77
"authors": [
88
{
99
"name": "Kristofers Ozolins"

etc/adminhtml/system.xml

+5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525
<can_be_empty>1</can_be_empty>
2626
<comment>Select attributes to run through CMS content parser/filter. For example, wysiwyg attributes</comment>
2727
</field>
28+
<field id="parse_category_media" translate="label comment" type="select" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
29+
<label>Convert Category Image Attribute Url</label>
30+
<comment>Converts Category Image URLs to VSF during indexation.</comment>
31+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
32+
</field>
2833
<field id="enabled" translate="label comment" type="select" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
2934
<label>Enable URL Rewrites for all content</label>
3035
<comment>Converts magento URLs to VSF during indexation.</comment>

etc/di.xml

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<argument name="dataProviders" xsi:type="array">
66
<item name="category" xsi:type="array">
77
<item name="wysiwyg_block_data_category" xsi:type="object">Magebit\StaticContentProcessor\Model\Indexer\DataProvider\Category\WysiwygBlockData</item>
8+
<item name="image_url_category" xsi:type="object">Magebit\StaticContentProcessor\Model\Indexer\DataProvider\Category\ImageUrlRewrite</item>
89
</item>
910
<item name="product" xsi:type="array">
1011
<item name="wysiwyg_block_data_product" xsi:type="object">Magebit\StaticContentProcessor\Model\Indexer\DataProvider\Product\WysiwygBlockData</item>

etc/module.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0"?>
22
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
3-
<module name="Magebit_StaticContentProcessor" setup_version="1.0.0">
3+
<module name="Magebit_StaticContentProcessor" setup_version="2.0.0">
44
<sequence>
55
<Divante_VsbridgeIndexerCms/>
66
</sequence>

0 commit comments

Comments
 (0)