Skip to content

Commit 125bff7

Browse files
committed
PB-370: Introduce upgrade mechanism for Page Builder content
- move utility function to Document class - replace usage of ObjectManager with Factory
1 parent 02c204d commit 125bff7

File tree

5 files changed

+39
-24
lines changed

5 files changed

+39
-24
lines changed

app/code/Magento/PageBuilder/Model/Dom/Adapter/DocumentInterface.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,4 +181,11 @@ public function getContents(): string;
181181
* value is found, or null if the key is not found.
182182
*/
183183
public function getMetadata($key = null);
184+
185+
/**
186+
* Strips the HTML doctype, body, etc. tags that are automatically wrapped around the content.
187+
*
188+
* @return string
189+
*/
190+
public function stripHtmlWrapperTags(): string;
184191
}

app/code/Magento/PageBuilder/Model/Dom/Document.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,4 +204,13 @@ public function getMetadata($key = null)
204204
{
205205
return $this->document->getMetadata($key);
206206
}
207+
208+
/**
209+
* @inheritDoc
210+
*/
211+
public function stripHtmlWrapperTags(): string
212+
{
213+
preg_match('/<body>(.*)<\/body>/', $this->getContents(), $matches);
214+
return isset($matches[1]) ? $matches[1] : '';
215+
}
207216
}

app/code/Magento/PageBuilder/Setup/Converters/FixFullWidthRowPadding.php

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,38 @@
88

99
namespace Magento\PageBuilder\Setup\Converters;
1010

11-
use Magento\Framework\App\ObjectManager;
1211
use Magento\Framework\DB\DataConverter\DataConverterInterface;
1312
use Magento\PageBuilder\Model\Dom\Adapter\ElementInterface;
14-
use Magento\PageBuilder\Model\Dom\Adapter\HtmlDocumentInterface;
1513
use Magento\PageBuilder\Model\Dom\HtmlDocument;
14+
use Magento\PageBuilder\Model\Dom\HtmlDocumentFactory;
1615

1716
/**
1817
* Converter to move padding in full width columns from the main row element to the inner element
1918
*/
2019
class FixFullWidthRowPadding implements DataConverterInterface
2120
{
21+
/**
22+
* @var HtmlDocumentFactory
23+
*/
24+
private $htmlDocumentFactory;
25+
26+
/**
27+
* @param HtmlDocumentFactory $htmlDocumentFactory
28+
*/
29+
public function __construct(HtmlDocumentFactory $htmlDocumentFactory)
30+
{
31+
$this->htmlDocumentFactory = $htmlDocumentFactory;
32+
}
33+
2234
/**
2335
* @inheritDoc
2436
*/
2537
public function convert($value)
2638
{
2739
/** @var HtmlDocument $document */
28-
$document = ObjectManager::getInstance()->create(
29-
HtmlDocumentInterface::class,
30-
[ 'document' => $value ]
31-
);
32-
// remove padding from main row element
40+
$document = $this->htmlDocumentFactory->create([ 'document' => $value ]);
3341
$fullWidthRows = $document->querySelectorAll("div[data-content-type='row'][data-appearance='full-width']");
34-
/**
35-
* @var ElementInterface $row
36-
*/
42+
/** @var ElementInterface $row */
3743
foreach ($fullWidthRows as $row) {
3844
$style = $row->getAttribute("style");
3945
preg_match("/padding:(.*?);/", $style, $results);
@@ -44,18 +50,6 @@ public function convert($value)
4450
$innerDiv = $row->querySelector(".row-full-width-inner");
4551
$innerDiv->addStyle("padding", $padding);
4652
}
47-
return $this->stripHtmlWrapperTags($document->getContents());
48-
}
49-
50-
/**
51-
* Strips the HTML doctype, body, etc. tags that are automatically wrapped around the content.
52-
*
53-
* @param string $content
54-
* @return string
55-
*/
56-
private function stripHtmlWrapperTags(string $content): string
57-
{
58-
preg_match('/<body>(.*)<\/body>/', $content, $matches);
59-
return isset($matches[1]) ? $matches[1] : '';
53+
return $document->stripHtmlWrapperTags();
6054
}
6155
}

app/code/Magento/PageBuilder/Setup/Patch/Data/UpdateContent.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use Magento\PageBuilder\Setup\UpgradeContentHelper;
1212

1313
/**
14-
* Patch is mechanism that allows us to do atomic upgrade data changes
14+
* Patch upgrade mechanism allows us to do atomic data changes
1515
*/
1616
class UpdateContent implements DataPatchInterface
1717
{

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,4 +298,9 @@
298298
</argument>
299299
</arguments>
300300
</type>
301+
<virtualType name="pageBuilderHtmlDocumentFactory" type="Magento\PageBuilder\Model\Dom\HtmlDocumentFactory">
302+
<arguments>
303+
<argument name="instanceName" xsi:type="string">Magento\PageBuilder\Model\Dom\HtmlDocument</argument>
304+
</arguments>
305+
</virtualType>
301306
</config>

0 commit comments

Comments
 (0)