Skip to content

Add Category Image Url Converter #2

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

Merged
merged 1 commit into from
Mar 8, 2021
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

#### [2.0.1] - 2021-03-08

##### Added
- Add Category Media Url Support

#### [1.0.0] - 2020-03-03

##### Added
Expand Down
131 changes: 131 additions & 0 deletions Model/Indexer/DataProvider/Category/ImageUrlRewrite.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php declare(strict_types=1);
/**
* This file is part of the Magebit_StaticContentProcessor package
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Magebit_StaticContentProcessor
* to newer versions in the future.
*
* @copyright Copyright (c) 2021 Magebit (https://magebit.com/)
* @author Liene Tunne <info@magebit.com>
* @license GNU General Public License ("GPL") v3.0
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Magebit\StaticContentProcessor\Model\Indexer\DataProvider\Category;

use Magento\Framework\UrlInterface;
use Magento\Store\Model\ScopeInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Divante\VsbridgeIndexerCore\Api\DataProviderInterface;
use Magento\Catalog\Model\Config;
use Magento\Framework\Exception\LocalizedException;

/**
* Class WysiwygBlockData
*/
class ImageUrlRewrite implements DataProviderInterface
{
/**
* Entity code
*/
const ENTITY = 'catalog_category';

/**
* @var ScopeConfigInterface
*/
private $scopeConfig;

/**
* @var Config
*/
private $catalogConfig;


/**
* ImageUrlRewrite constructor.
* @param ScopeConfigInterface $scopeConfig
* @param Config $catalogConfig
*/
public function __construct(
ScopeConfigInterface $scopeConfig,
Config $catalogConfig
) {
$this->scopeConfig = $scopeConfig;
$this->catalogConfig = $catalogConfig;
}

/**
* @param array $indexData
* @param int $storeId
* @return array
* @throws LocalizedException
*/
public function addData(array $indexData, $storeId): array
{
$processable = [];

$rewriteCatMediaUrl = $this->scopeConfig->getValue(
'vsbridge_indexer_settings/url_rewrites/parse_category_media',
ScopeInterface::SCOPE_STORE
);

if ($rewriteCatMediaUrl) {
foreach ($indexData as $categoryId => $categoryData) {
foreach ($categoryData as $attributeCode => $attribute) {

if ($this->checkAttributeIsImage($attributeCode)) {
$processable[] = [
'categoryId' => $categoryId,
'attributeId' => $attributeCode,
'content' => $attribute
];
}
}
}

$filteredIndexData = $this->convertMediaUrl($processable);

foreach ($filteredIndexData as $data) {
$indexData[$data['categoryId']][$data['attributeId']] = $data['content'];
}
}

return $indexData;
}

/**
* @param string|null $attributeCode
* @return bool
* @throws LocalizedException
*/
protected function checkAttributeIsImage(?string $attributeCode): bool
{
$attributeData = $this->catalogConfig->getAttribute(self::ENTITY, $attributeCode);

return $attributeData->getFrontendInput() == 'image' ? true : false;
}

/**
* @param array $data
* @return array
*/
protected function convertMediaUrl(array $data): array
{
$urlRewritesMedia = $this->scopeConfig->getValue(
'vsbridge_indexer_settings/url_rewrites/media_url',
ScopeInterface::SCOPE_STORE
);

foreach ($data as &$attribute) {
// Remove store Url
$cleanVSFMediaPath = substr($urlRewritesMedia, strpos($urlRewritesMedia, '/media'));
$attribute['content'] = str_replace('/media', $cleanVSFMediaPath, $attribute['content']);
}

return $data;
}
}
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ You can find these configuration fields in:
You can also specify which category and product attributes to run through the processor. As an example, you
could select product `description` attribute and all the links and images will be converted with VSF urls.

You can also enable Category Image attribute url processor. It will convert all category attributes as image to VSF urls.

#### As a dependency

You can also use this module as a dependency for your own module:
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Converts magento URL's to VSF during indexation",
"license": "GPL-3.0-only",
"type": "magento2-component",
"version": "2.0.0",
"version": "2.0.1",
"authors": [
{
"name": "Kristofers Ozolins"
Expand Down
5 changes: 5 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
<can_be_empty>1</can_be_empty>
<comment>Select attributes to run through CMS content parser/filter. For example, wysiwyg attributes</comment>
</field>
<field id="parse_category_media" translate="label comment" type="select" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Convert Category Image Attribute Url</label>
<comment>Converts Category Image URLs to VSF during indexation.</comment>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="enabled" translate="label comment" type="select" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Enable URL Rewrites for all content</label>
<comment>Converts magento URLs to VSF during indexation.</comment>
Expand Down
1 change: 1 addition & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<argument name="dataProviders" xsi:type="array">
<item name="category" xsi:type="array">
<item name="wysiwyg_block_data_category" xsi:type="object">Magebit\StaticContentProcessor\Model\Indexer\DataProvider\Category\WysiwygBlockData</item>
<item name="image_url_category" xsi:type="object">Magebit\StaticContentProcessor\Model\Indexer\DataProvider\Category\ImageUrlRewrite</item>
</item>
<item name="product" xsi:type="array">
<item name="wysiwyg_block_data_product" xsi:type="object">Magebit\StaticContentProcessor\Model\Indexer\DataProvider\Product\WysiwygBlockData</item>
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Magebit_StaticContentProcessor" setup_version="1.0.0">
<module name="Magebit_StaticContentProcessor" setup_version="2.0.0">
<sequence>
<Divante_VsbridgeIndexerCms/>
</sequence>
Expand Down