Skip to content

Commit c2c7128

Browse files
committed
MC-3784: Implementation of Preview renderer
- Added renderer support for dynamic block
1 parent 206cb78 commit c2c7128

File tree

5 files changed

+71
-28
lines changed

5 files changed

+71
-28
lines changed

app/code/Magento/PageBuilder/Model/Stage/Renderer/CmsStaticBlock.php

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,29 @@ class CmsStaticBlock implements \Magento\PageBuilder\Model\Stage\RendererInterfa
3232
*/
3333
private $loggerInterface;
3434

35+
/**
36+
* @var \Magento\PageBuilder\Model\Stage\ScriptFilter
37+
*/
38+
private $scriptFilter;
39+
3540
/**
3641
* CmsStaticBlock constructor.
3742
*
3843
* @param \Magento\Cms\Model\ResourceModel\Block\CollectionFactory $blockCollectionFactory
3944
* @param WidgetDirective $widgetDirectiveRenderer
4045
* @param LoggerInterface $loggerInterface
46+
* @param \Magento\PageBuilder\Model\Stage\ScriptFilter $scriptFilter
4147
*/
4248
public function __construct(
4349
\Magento\Cms\Model\ResourceModel\Block\CollectionFactory $blockCollectionFactory,
4450
WidgetDirective $widgetDirectiveRenderer,
45-
LoggerInterface $loggerInterface
51+
LoggerInterface $loggerInterface,
52+
\Magento\PageBuilder\Model\Stage\ScriptFilter $scriptFilter
4653
) {
4754
$this->blockCollectionFactory = $blockCollectionFactory;
4855
$this->widgetDirectiveRenderer = $widgetDirectiveRenderer;
4956
$this->loggerInterface = $loggerInterface;
57+
$this->scriptFilter = $scriptFilter;
5058
}
5159

5260
/**
@@ -88,34 +96,11 @@ public function render(array $params): array
8896

8997
if ($block->isActive()) {
9098
$directiveResult = $this->widgetDirectiveRenderer->render($params);
91-
$result['content'] = $this->removeScriptTags($directiveResult['content']);
99+
$result['content'] = $this->scriptFilter->removeScriptTags($directiveResult['content']);
92100
} else {
93101
$result['error'] = __('Block disabled');
94102
}
95103

96104
return $result;
97105
}
98-
99-
/**
100-
* Remove script tag from html
101-
*
102-
* @param string $content
103-
* @return string
104-
*/
105-
private function removeScriptTags(string $content): string
106-
{
107-
$dom = new \DOMDocument();
108-
try {
109-
//this code is required because of https://bugs.php.net/bug.php?id=60021
110-
$previous = libxml_use_internal_errors(true);
111-
$dom->loadHTML($content);
112-
} catch (\Exception $e) {
113-
$this->loggerInterface->critical($e->getMessage());
114-
}
115-
libxml_use_internal_errors($previous);
116-
foreach (iterator_to_array($dom->getElementsByTagName('script')) as $item) {
117-
$item->parentNode->removeChild($item);
118-
}
119-
return $dom->saveHTML();
120-
}
121106
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\PageBuilder\Model\Stage;
10+
11+
use Psr\Log\LoggerInterface;
12+
13+
/**
14+
* Filters script tags from stage output
15+
*
16+
* @api
17+
*/
18+
class ScriptFilter
19+
{
20+
/**
21+
* @var LoggerInterface
22+
*/
23+
private $loggerInterface;
24+
25+
/**
26+
* ScriptFilter constructor.
27+
* @param LoggerInterface $loggerInterface
28+
*/
29+
public function __construct(
30+
LoggerInterface $loggerInterface
31+
) {
32+
$this->loggerInterface = $loggerInterface;
33+
}
34+
35+
/**
36+
* Remove script tag from html
37+
*
38+
* @param string $content
39+
* @return string
40+
*/
41+
public function removeScriptTags(string $content): string
42+
{
43+
$dom = new \DOMDocument();
44+
try {
45+
//this code is required because of https://bugs.php.net/bug.php?id=60021
46+
$previous = libxml_use_internal_errors(true);
47+
$dom->loadHTML($content);
48+
} catch (\Exception $e) {
49+
$this->loggerInterface->critical($e->getMessage());
50+
}
51+
libxml_use_internal_errors($previous);
52+
foreach (iterator_to_array($dom->getElementsByTagName('script')) as $item) {
53+
$item->parentNode->removeChild($item);
54+
}
55+
return $dom->saveHTML();
56+
}
57+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
<item name="default" xsi:type="object">Magento\PageBuilder\Model\Stage\Renderer\Block</item>
6464
<item name="products" xsi:type="object">Magento\PageBuilder\Model\Stage\Renderer\WidgetDirective</item>
6565
<item name="block" xsi:type="object">Magento\PageBuilder\Model\Stage\Renderer\CmsStaticBlock</item>
66+
<item name="dynamic_block" xsi:type="object">Magento\BannerPageBuilder\Model\Stage\Renderer\DynamicBlock</item>
6667
</argument>
6768
</arguments>
6869
</type>

app/code/Magento/PageBuilder/view/adminhtml/web/js/form/element/block-chooser.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ define([
116116
*
117117
* @returns {String}
118118
*/
119-
getStatusLabel: function () {
120-
return this.meta()['is_active'] === '1' ? $t('Active') : $t('Inactive');
119+
getStatusLabel: function (field, enabledOption, disabledOption) {
120+
return this.meta()[field] === '1' ? $t(enabledOption) : $t(disabledOption);
121121
}
122122
});
123123
});

app/code/Magento/PageBuilder/view/adminhtml/web/template/form/element/block-chooser.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<div class="data-grid-cell-content"><strong translate="'Status'"></strong></div>
2727
</td>
2828
<td>
29-
<div class="data-grid-cell-content" text="getStatusLabel()"></div>
29+
<div class="data-grid-cell-content" text="getStatusLabel('is_active', 'Active', 'Inactive')"></div>
3030
</td>
3131
</tr>
3232
</tbody>

0 commit comments

Comments
 (0)