Skip to content

Commit 7914804

Browse files
committed
PB-370: Introduce upgrade mechanism for Page Builder content
- upgrade framework and data patch for PB-55
1 parent 3423a59 commit 7914804

File tree

5 files changed

+155
-37
lines changed

5 files changed

+155
-37
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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\Setup\Converters;
10+
11+
use Magento\Framework\App\ObjectManager;
12+
use Magento\Framework\DB\DataConverter\DataConverterInterface;
13+
use Magento\PageBuilder\Model\Dom\Adapter\ElementInterface;
14+
use Magento\PageBuilder\Model\Dom\Adapter\HtmlDocumentInterface;
15+
use Magento\PageBuilder\Model\Dom\HtmlDocument;
16+
17+
/**
18+
* Converter to move padding in full width columns from the main row element to the inner element
19+
*/
20+
class FixFullWidthRowPadding implements DataConverterInterface
21+
{
22+
/**
23+
* @inheritDoc
24+
*/
25+
public function convert($value)
26+
{
27+
/** @var HtmlDocument $document */
28+
$document = ObjectManager::getInstance()->create(
29+
HtmlDocumentInterface::class,
30+
[ 'document' => $value ]
31+
);
32+
// remove padding from main row element
33+
$fullWidthRows = $document->querySelectorAll("div[data-content-type='row'][data-appearance='full-width']");
34+
/** @var ElementInterface $row */
35+
foreach ($fullWidthRows as $row) {
36+
$style = $row->getAttribute("style");
37+
preg_match("/padding:(.*?);/", $style, $results);
38+
$padding = isset($results[0]) ? $results[0] : '';
39+
// remove padding from main row element
40+
$row->setAttribute("style", preg_replace("/padding:(.*?);/", "", $style));
41+
// add padding to inner row element
42+
/** @var ElementInterface $innerDiv */
43+
$innerDiv = $row->querySelector(".row-full-width-inner");
44+
$innerDiv->setAttribute("style", $padding . $innerDiv->getAttribute("style") ?: '');
45+
}
46+
//strip the added html content
47+
preg_match('/<body>(.*)<\/body>/', $document->getContents(), $matches);
48+
return isset($matches[1]) ? $matches[1] : '';
49+
}
50+
}

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

Lines changed: 0 additions & 19 deletions
This file was deleted.

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
*/
66
namespace Magento\PageBuilder\Setup\Patch\Data;
77

8-
use Magento\Framework\Setup\Patch\DataPatchInterface;
98
use Magento\Framework\DB\FieldDataConversionException;
9+
use Magento\Framework\Setup\Patch\DataPatchInterface;
10+
use Magento\PageBuilder\Setup\Converters\FixFullWidthRowPadding;
1011
use Magento\PageBuilder\Setup\UpgradeContentHelper;
11-
use Magento\PageBuilder\Setup\Converters\MoveAttribute;
1212

1313
/**
14-
* Patch is mechanism, that allows to do atomic upgrade data changes
14+
* Patch is mechanism that allows us to do atomic upgrade data changes
1515
*/
1616
class UpdateContent implements DataPatchInterface
1717
{
@@ -30,31 +30,28 @@ public function __construct(
3030
}
3131

3232
/**
33-
* Do Upgrade
33+
* Do upgrade
3434
*
3535
* @return void
36-
*/
37-
/**
38-
* @return DataPatchInterface|void
3936
* @throws FieldDataConversionException
4037
*/
4138
public function apply()
4239
{
4340
$this->helper->upgrade([
44-
MoveAttribute::class
41+
FixFullWidthRowPadding::class
4542
]);
4643
}
4744

4845
/**
49-
* {@inheritdoc}
46+
* @inheritdoc
5047
*/
5148
public function getAliases()
5249
{
5350
return [];
5451
}
5552

5653
/**
57-
* {@inheritdoc}
54+
* @inheritdoc
5855
*/
5956
public static function getDependencies()
6057
{

app/code/Magento/PageBuilder/Setup/UpgradeContentHelper.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,20 @@
55
*/
66
namespace Magento\PageBuilder\Setup;
77

8-
use Magento\Framework\DB\Select\QueryModifierFactory;
9-
use Magento\Framework\DB\FieldToConvert;
10-
use Magento\Framework\Setup\ModuleDataSetupInterface;
118
use Magento\Framework\DB\AggregatedFieldDataConverter;
129
use Magento\Framework\DB\FieldDataConversionException;
10+
use Magento\Framework\DB\FieldToConvert;
11+
use Magento\Framework\DB\Select\QueryModifierFactory;
12+
use Magento\Framework\Setup\ModuleDataSetupInterface;
1313
use Magento\PageBuilder\Model\UpgradableEntitiesPool;
1414

15+
/**
16+
* Helper class to run collection of converters
17+
*/
1518
class UpgradeContentHelper
1619
{
1720
public const PAGE_BUILDER_CONTENT_PATTERN = '%data-content-type="%';
21+
1822
/**
1923
* @var ModuleDataSetupInterface $moduleDataSetup
2024
*/
@@ -36,7 +40,8 @@ class UpgradeContentHelper
3640
private $aggregatedFieldDataConverter;
3741

3842
/**
39-
* UpdateContent constructor.
43+
* UpgradeContentHelper constructor.
44+
*
4045
* @param ModuleDataSetupInterface $moduleDataSetup
4146
* @param QueryModifierFactory $queryModifierFactory
4247
* @param UpgradableEntitiesPool $entitiesPool
@@ -55,9 +60,8 @@ public function __construct(
5560
}
5661

5762
/**
58-
* @param array $converters
59-
*/
60-
/**
63+
* Executes each specified converter against all upgradable fields in the database
64+
*
6165
* @param array $converters
6266
* @throws FieldDataConversionException
6367
*/
@@ -97,4 +101,4 @@ public function upgrade(array $converters): void
97101
$this->aggregatedFieldDataConverter->convert($fields, $this->moduleDataSetup->getConnection());
98102
}
99103
}
100-
}
104+
}

0 commit comments

Comments
 (0)