Skip to content

Commit bdcfbc8

Browse files
committed
MC-3062: Implement Always Rendering Magento directives on Storefront
1 parent 113bf62 commit bdcfbc8

File tree

6 files changed

+120
-16
lines changed

6 files changed

+120
-16
lines changed

app/code/Magento/Catalog/Helper/Data.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper
3232

3333
const CONFIG_USE_STATIC_URLS = 'cms/wysiwyg/use_static_urls_in_catalog';
3434

35+
/**
36+
* @deprecated
37+
* @see \Magento\Catalog\Helper\Output::isDirectivesExists
38+
*/
3539
const CONFIG_PARSE_URL_DIRECTIVES = 'catalog/frontend/parse_url_directives';
3640

3741
const XML_PATH_DISPLAY_PRODUCT_COUNT = 'catalog/layered_navigation/display_product_count';
@@ -443,6 +447,8 @@ public function isUsingStaticUrlsAllowed()
443447
* Check if the parsing of URL directives is allowed for the catalog
444448
*
445449
* @return bool
450+
* @deprecated
451+
* @see \Magento\Catalog\Helper\Output::isDirectivesExists
446452
*/
447453
public function isUrlDirectivesParsingAllowed()
448454
{
@@ -457,6 +463,7 @@ public function isUrlDirectivesParsingAllowed()
457463
* Retrieve template processor for catalog content
458464
*
459465
* @return \Magento\Framework\Filter\Template
466+
* @throws \Magento\Framework\Exception\LocalizedException
460467
*/
461468
public function getPageTemplateProcessor()
462469
{

app/code/Magento/Catalog/Helper/Output.php

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,29 @@ class Output extends \Magento\Framework\App\Helper\AbstractHelper
4545
protected $_escaper;
4646

4747
/**
48+
* @var array
49+
*/
50+
private $directivePatterns;
51+
52+
/**
53+
* Output constructor.
4854
* @param \Magento\Framework\App\Helper\Context $context
4955
* @param \Magento\Eav\Model\Config $eavConfig
5056
* @param Data $catalogData
5157
* @param \Magento\Framework\Escaper $escaper
58+
* @param array $directivePatterns
5259
*/
5360
public function __construct(
5461
\Magento\Framework\App\Helper\Context $context,
5562
\Magento\Eav\Model\Config $eavConfig,
5663
Data $catalogData,
57-
\Magento\Framework\Escaper $escaper
64+
\Magento\Framework\Escaper $escaper,
65+
$directivePatterns = []
5866
) {
5967
$this->_eavConfig = $eavConfig;
6068
$this->_catalogData = $catalogData;
6169
$this->_escaper = $escaper;
70+
$this->directivePatterns = $directivePatterns;
6271
parent::__construct($context);
6372
}
6473

@@ -134,6 +143,7 @@ public function process($method, $result, $params)
134143
* @param string $attributeName
135144
* @return string
136145
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
146+
* @throws \Magento\Framework\Exception\LocalizedException
137147
*/
138148
public function productAttribute($product, $attributeHtml, $attributeName)
139149
{
@@ -151,10 +161,12 @@ public function productAttribute($product, $attributeHtml, $attributeName)
151161
$attributeHtml = nl2br($attributeHtml);
152162
}
153163
}
154-
if ($attribute->getIsHtmlAllowedOnFront() && $attribute->getIsWysiwygEnabled()) {
155-
if ($this->_catalogData->isUrlDirectivesParsingAllowed()) {
156-
$attributeHtml = $this->_getTemplateProcessor()->filter($attributeHtml);
157-
}
164+
if ($attributeHtml !== null
165+
&& $attribute->getIsHtmlAllowedOnFront()
166+
&& $attribute->getIsWysiwygEnabled()
167+
&& $this->isDirectivesExists($attributeHtml)
168+
) {
169+
$attributeHtml = $this->_getTemplateProcessor()->filter($attributeHtml);
158170
}
159171

160172
$attributeHtml = $this->process(
@@ -173,6 +185,7 @@ public function productAttribute($product, $attributeHtml, $attributeName)
173185
* @param string $attributeHtml
174186
* @param string $attributeName
175187
* @return string
188+
* @throws \Magento\Framework\Exception\LocalizedException
176189
*/
177190
public function categoryAttribute($category, $attributeHtml, $attributeName)
178191
{
@@ -185,10 +198,13 @@ public function categoryAttribute($category, $attributeHtml, $attributeName)
185198
) {
186199
$attributeHtml = $this->_escaper->escapeHtml($attributeHtml);
187200
}
188-
if ($attribute->getIsHtmlAllowedOnFront() && $attribute->getIsWysiwygEnabled()) {
189-
if ($this->_catalogData->isUrlDirectivesParsingAllowed()) {
190-
$attributeHtml = $this->_getTemplateProcessor()->filter($attributeHtml);
191-
}
201+
if ($attributeHtml !== null
202+
&& $attribute->getIsHtmlAllowedOnFront()
203+
&& $attribute->getIsWysiwygEnabled()
204+
&& $this->isDirectivesExists($attributeHtml)
205+
206+
) {
207+
$attributeHtml = $this->_getTemplateProcessor()->filter($attributeHtml);
192208
}
193209
$attributeHtml = $this->process(
194210
'categoryAttribute',
@@ -197,4 +213,23 @@ public function categoryAttribute($category, $attributeHtml, $attributeName)
197213
);
198214
return $attributeHtml;
199215
}
216+
217+
/**
218+
* Check if string has directives
219+
*
220+
* @param string $attributeHtml
221+
* @return bool
222+
*/
223+
public function isDirectivesExists($attributeHtml)
224+
{
225+
$matches = false;
226+
foreach ($this->directivePatterns as $pattern)
227+
{
228+
if (preg_match($pattern, $attributeHtml))
229+
{
230+
$matches = true;
231+
}
232+
}
233+
return $matches;
234+
}
200235
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Catalog\Setup\Patch\Data;
8+
9+
use Magento\Framework\Setup\ModuleDataSetupInterface;
10+
use Magento\Framework\Setup\Patch\DataPatchInterface;
11+
12+
/**
13+
* Class EnableDirectiveParsing
14+
* @package Magento\Catalog\Setup\Patch
15+
*/
16+
class EnableDirectiveParsing implements DataPatchInterface
17+
{
18+
/**
19+
* @var ModuleDataSetupInterface
20+
*/
21+
private $moduleDataSetup;
22+
23+
/**
24+
* PatchInitial constructor.
25+
* @param ModuleDataSetupInterface $moduleDataSetup
26+
*/
27+
public function __construct(
28+
ModuleDataSetupInterface $moduleDataSetup
29+
) {
30+
$this->moduleDataSetup = $moduleDataSetup;
31+
}
32+
33+
/**
34+
* {@inheritdoc}
35+
*/
36+
public function apply()
37+
{
38+
$configTable = $this->moduleDataSetup->getTable('core_config_data');
39+
$this->moduleDataSetup->getConnection()->update(
40+
$configTable,
41+
['value' => new \Zend_Db_Expr('1')],
42+
['path = ?' => \Magento\Catalog\Helper\Data::CONFIG_PARSE_URL_DIRECTIVES, 'value IN (?)' => '0']
43+
);
44+
}
45+
46+
/**
47+
* {@inheritdoc}
48+
*/
49+
public static function getDependencies()
50+
{
51+
return [];
52+
}
53+
54+
/**
55+
* {@inheritdoc}
56+
*/
57+
public function getAliases()
58+
{
59+
return [];
60+
}
61+
}

app/code/Magento/Catalog/etc/adminhtml/system.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,6 @@
9292
<comment>Whether to show "All" option in the "Show X Per Page" dropdown</comment>
9393
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
9494
</field>
95-
<field id="parse_url_directives" translate="label comment" type="select" sortOrder="200" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
96-
<label>Allow Dynamic Media URLs</label>
97-
<comment>E.g. {{media url="path/to/image.jpg"}} {{skin url="path/to/picture.gif"}}. Dynamic directives parsing impacts catalog performance.</comment>
98-
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
99-
</field>
10095
</group>
10196
<group id="placeholder" translate="label" sortOrder="300" showInDefault="1" showInWebsite="1" showInStore="1">
10297
<label>Product Image Placeholders</label>

app/code/Magento/Catalog/etc/di.xml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,17 @@
173173
</type>
174174
<type name="Magento\Catalog\Helper\Data">
175175
<arguments>
176-
<argument name="templateFilterModel" xsi:type="string">Magento\Catalog\Model\Template\Filter</argument>
176+
<argument name="templateFilterModel" xsi:type="string">Magento\Widget\Model\Template\Filter</argument>
177177
<argument name="catalogSession" xsi:type="object">Magento\Catalog\Model\Session\Proxy</argument>
178178
</arguments>
179179
</type>
180+
<type name="Magento\Catalog\Helper\Output">
181+
<arguments>
182+
<argument name="directivePatterns" xsi:type="array">
183+
<item name="construct" xsi:type="const">\Magento\Framework\Filter\Template::CONSTRUCTION_PATTERN</item>
184+
</argument>
185+
</arguments>
186+
</type>
180187
<type name="Magento\Catalog\Model\Config\Source\GridPerPage">
181188
<arguments>
182189
<argument name="perPageValues" xsi:type="string">9,15,30</argument>

app/code/Magento/Catalog/i18n/en_US.csv

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,6 @@ Comma-separated.,Comma-separated.
658658
"Product Listing Sort by","Product Listing Sort by"
659659
"Allow All Products per Page","Allow All Products per Page"
660660
"Whether to show ""All"" option in the ""Show X Per Page"" dropdown","Whether to show ""All"" option in the ""Show X Per Page"" dropdown"
661-
"Allow Dynamic Media URLs","Allow Dynamic Media URLs"
662661
"E.g. {{media url=""path/to/image.jpg""}} {{skin url=""path/to/picture.gif""}}. Dynamic directives parsing impacts catalog performance.","E.g. {{media url=""path/to/image.jpg""}} {{skin url=""path/to/picture.gif""}}. Dynamic directives parsing impacts catalog performance."
663662
"Product Image Placeholders","Product Image Placeholders"
664663
"Search Engine Optimization","Search Engine Optimization"

0 commit comments

Comments
 (0)