Skip to content

Commit e5208f0

Browse files
ENGCOM-8576: FIX Perfomance Issue for Backend EDIT of CMS #30936 #30943
2 parents 0d98fdc + 6effeb4 commit e5208f0

File tree

2 files changed

+135
-88
lines changed

2 files changed

+135
-88
lines changed

app/code/Magento/Cms/Model/Page/DataProvider.php

Lines changed: 80 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,24 @@
55
*/
66
namespace Magento\Cms\Model\Page;
77

8-
use Magento\Cms\Model\Page;
8+
use Magento\Cms\Api\Data\PageInterface;
9+
use Magento\Cms\Api\PageRepositoryInterface;
10+
use Magento\Cms\Model\PageFactory;
911
use Magento\Cms\Model\ResourceModel\Page\CollectionFactory;
1012
use Magento\Framework\App\ObjectManager;
1113
use Magento\Framework\App\Request\DataPersistorInterface;
1214
use Magento\Framework\App\RequestInterface;
13-
use Magento\Ui\DataProvider\Modifier\PoolInterface;
1415
use Magento\Framework\AuthorizationInterface;
16+
use Magento\Framework\Exception\LocalizedException;
17+
use Magento\Ui\DataProvider\Modifier\PoolInterface;
18+
use Magento\Ui\DataProvider\ModifierPoolDataProvider;
19+
use Psr\Log\LoggerInterface;
1520

1621
/**
17-
* Class DataProvider
22+
* Cms Page DataProvider
1823
*/
19-
class DataProvider extends \Magento\Ui\DataProvider\ModifierPoolDataProvider
24+
class DataProvider extends ModifierPoolDataProvider
2025
{
21-
/**
22-
* @var \Magento\Cms\Model\ResourceModel\Page\Collection
23-
*/
24-
protected $collection;
25-
2626
/**
2727
* @var DataPersistorInterface
2828
*/
@@ -33,6 +33,11 @@ class DataProvider extends \Magento\Ui\DataProvider\ModifierPoolDataProvider
3333
*/
3434
protected $loadedData;
3535

36+
/**
37+
* @var PageRepositoryInterface
38+
*/
39+
private $pageRepository;
40+
3641
/**
3742
* @var AuthorizationInterface
3843
*/
@@ -49,9 +54,14 @@ class DataProvider extends \Magento\Ui\DataProvider\ModifierPoolDataProvider
4954
private $customLayoutManager;
5055

5156
/**
52-
* @var CollectionFactory
57+
* @var PageFactory
58+
*/
59+
private $pageFactory;
60+
61+
/**
62+
* @var LoggerInterface
5363
*/
54-
private $collectionFactory;
64+
private $logger;
5565

5666
/**
5767
* @param string $name
@@ -65,6 +75,9 @@ class DataProvider extends \Magento\Ui\DataProvider\ModifierPoolDataProvider
6575
* @param AuthorizationInterface|null $auth
6676
* @param RequestInterface|null $request
6777
* @param CustomLayoutManagerInterface|null $customLayoutManager
78+
* @param PageRepositoryInterface|null $pageRepository
79+
* @param PageFactory|null $pageFactory
80+
* @param LoggerInterface|null $logger
6881
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
6982
*/
7083
public function __construct(
@@ -78,33 +91,22 @@ public function __construct(
7891
PoolInterface $pool = null,
7992
?AuthorizationInterface $auth = null,
8093
?RequestInterface $request = null,
81-
?CustomLayoutManagerInterface $customLayoutManager = null
94+
?CustomLayoutManagerInterface $customLayoutManager = null,
95+
?PageRepositoryInterface $pageRepository = null,
96+
?PageFactory $pageFactory = null,
97+
?LoggerInterface $logger = null
8298
) {
99+
parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data, $pool);
83100
$this->collection = $pageCollectionFactory->create();
84-
$this->collectionFactory = $pageCollectionFactory;
85101
$this->dataPersistor = $dataPersistor;
86-
parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data, $pool);
87102
$this->auth = $auth ?? ObjectManager::getInstance()->get(AuthorizationInterface::class);
88103
$this->meta = $this->prepareMeta($this->meta);
89104
$this->request = $request ?? ObjectManager::getInstance()->get(RequestInterface::class);
90105
$this->customLayoutManager = $customLayoutManager
91106
?? ObjectManager::getInstance()->get(CustomLayoutManagerInterface::class);
92-
}
93-
94-
/**
95-
* Find requested page.
96-
*
97-
* @return Page|null
98-
*/
99-
private function findCurrentPage(): ?Page
100-
{
101-
if ($this->getRequestFieldName() && ($pageId = (int)$this->request->getParam($this->getRequestFieldName()))) {
102-
//Loading data for the collection.
103-
$this->getData();
104-
return $this->collection->getItemById($pageId);
105-
}
106-
107-
return null;
107+
$this->pageRepository = $pageRepository ?? ObjectManager::getInstance()->get(PageRepositoryInterface::class);
108+
$this->pageFactory = $pageFactory ?: ObjectManager::getInstance()->get(PageFactory::class);
109+
$this->logger = $logger ?: ObjectManager::getInstance()->get(LoggerInterface::class);
108110
}
109111

110112
/**
@@ -128,29 +130,53 @@ public function getData()
128130
if (isset($this->loadedData)) {
129131
return $this->loadedData;
130132
}
131-
$this->collection = $this->collectionFactory->create();
132-
$items = $this->collection->getItems();
133-
/** @var $page \Magento\Cms\Model\Page */
134-
foreach ($items as $page) {
135-
$this->loadedData[$page->getId()] = $page->getData();
136-
if ($page->getCustomLayoutUpdateXml() || $page->getLayoutUpdateXml()) {
137-
//Deprecated layout update exists.
138-
$this->loadedData[$page->getId()]['layout_update_selected'] = '_existing_';
133+
134+
$page = $this->getCurrentPage();
135+
$this->loadedData[$page->getId()] = $page->getData();
136+
if ($page->getCustomLayoutUpdateXml() || $page->getLayoutUpdateXml()) {
137+
//Deprecated layout update exists.
138+
$this->loadedData[$page->getId()]['layout_update_selected'] = '_existing_';
139+
}
140+
141+
return $this->loadedData;
142+
}
143+
144+
/**
145+
* Return current page
146+
*
147+
* @return PageInterface
148+
*/
149+
private function getCurrentPage(): PageInterface
150+
{
151+
$pageId = $this->getPageId();
152+
if ($pageId) {
153+
try {
154+
$page = $this->pageRepository->getById($pageId);
155+
} catch (LocalizedException $exception) {
156+
$page = $this->pageFactory->create();
139157
}
158+
159+
return $page;
140160
}
141161

142162
$data = $this->dataPersistor->get('cms_page');
143-
if (!empty($data)) {
144-
$page = $this->collection->getNewEmptyItem();
145-
$page->setData($data);
146-
$this->loadedData[$page->getId()] = $page->getData();
147-
if ($page->getCustomLayoutUpdateXml() || $page->getLayoutUpdateXml()) {
148-
$this->loadedData[$page->getId()]['layout_update_selected'] = '_existing_';
149-
}
150-
$this->dataPersistor->clear('cms_page');
163+
if (empty($data)) {
164+
return $this->pageFactory->create();
151165
}
166+
$this->dataPersistor->clear('cms_page');
152167

153-
return $this->loadedData;
168+
return $this->pageFactory->create()
169+
->setData($data);
170+
}
171+
172+
/**
173+
* Returns current page id from request
174+
*
175+
* @return int
176+
*/
177+
private function getPageId(): int
178+
{
179+
return (int) $this->request->getParam($this->getRequestFieldName());
154180
}
155181

156182
/**
@@ -186,16 +212,20 @@ public function getMeta()
186212

187213
//List of custom layout files available for current page.
188214
$options = [['label' => 'No update', 'value' => '_no_update_']];
189-
if ($page = $this->findCurrentPage()) {
190-
//We must have a specific page selected.
191-
//If custom layout XML is set then displaying this special option.
215+
216+
$page = null;
217+
try {
218+
$page = $this->pageRepository->getById($this->getPageId());
192219
if ($page->getCustomLayoutUpdateXml() || $page->getLayoutUpdateXml()) {
193220
$options[] = ['label' => 'Use existing layout update XML', 'value' => '_existing_'];
194221
}
195222
foreach ($this->customLayoutManager->fetchAvailableFiles($page) as $layoutFile) {
196223
$options[] = ['label' => $layoutFile, 'value' => $layoutFile];
197224
}
225+
} catch (LocalizedException $e) {
226+
$this->logger->error($e->getMessage());
198227
}
228+
199229
$customLayoutMeta = [
200230
'design' => [
201231
'children' => [

dev/tests/integration/testsuite/Magento/Cms/Model/Page/DataProviderTest.php

Lines changed: 55 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@
99
namespace Magento\Cms\Model\Page;
1010

1111
use Magento\Cms\Api\GetPageByIdentifierInterface;
12+
use Magento\Framework\App\Request\Http as HttpRequest;
13+
use Magento\Framework\App\RequestInterface;
14+
use Magento\Framework\ObjectManagerInterface;
1215
use Magento\TestFramework\Cms\Model\CustomLayoutManager;
1316
use Magento\TestFramework\Helper\Bootstrap;
1417
use PHPUnit\Framework\TestCase;
15-
use Magento\Cms\Model\Page as PageModel;
16-
use Magento\Framework\App\Request\Http as HttpRequest;
1718

1819
/**
1920
* Test pages data provider.
@@ -22,6 +23,12 @@
2223
*/
2324
class DataProviderTest extends TestCase
2425
{
26+
private $providerData = [
27+
'name' => 'test',
28+
'primaryFieldName' => 'page_id',
29+
'requestFieldName' => 'page_id',
30+
];
31+
2532
/**
2633
* @var DataProvider
2734
*/
@@ -42,59 +49,69 @@ class DataProviderTest extends TestCase
4249
*/
4350
private $request;
4451

52+
/**
53+
* @var ObjectManagerInterface
54+
*/
55+
private $objectManager;
56+
4557
/**
4658
* @inheritDoc
4759
*/
4860
protected function setUp(): void
4961
{
50-
$objectManager = Bootstrap::getObjectManager();
51-
$objectManager->configure([
52-
'preferences' => [
53-
\Magento\Cms\Model\Page\CustomLayoutManagerInterface::class =>
54-
\Magento\TestFramework\Cms\Model\CustomLayoutManager::class
55-
]
62+
$this->objectManager = Bootstrap::getObjectManager();
63+
$this->objectManager->configure([
64+
'preferences' => [CustomLayoutManagerInterface::class => CustomLayoutManager::class]
5665
]);
57-
$this->repo = $objectManager->get(GetPageByIdentifierInterface::class);
58-
$this->filesFaker = $objectManager->get(CustomLayoutManager::class);
59-
$this->request = $objectManager->get(HttpRequest::class);
60-
$this->provider = $objectManager->create(
66+
$this->repo = $this->objectManager->get(GetPageByIdentifierInterface::class);
67+
$this->filesFaker = $this->objectManager->get(CustomLayoutManager::class);
68+
$this->request = $this->objectManager->get(HttpRequest::class);
69+
$this->provider = $this->objectManager->create(
6170
DataProvider::class,
62-
[
63-
'name' => 'test',
64-
'primaryFieldName' => 'page_id',
65-
'requestFieldName' => 'page_id',
66-
'customLayoutManager' => $this->filesFaker
67-
]
71+
array_merge($this->providerData, ['customLayoutManager' => $this->filesFaker])
6872
);
6973
}
7074

7175
/**
7276
* Check that custom layout date is handled properly.
7377
*
7478
* @magentoDataFixture Magento/Cms/_files/pages_with_layout_xml.php
75-
* @throws \Throwable
79+
* @dataProvider customLayoutDataProvider
80+
*
81+
* @param string $identifier
82+
* @param string|null $layoutUpdateSelected
7683
* @return void
7784
*/
78-
public function testCustomLayoutData(): void
85+
public function testCustomLayoutData(string $identifier, ?string $layoutUpdateSelected): void
7986
{
80-
$data = $this->provider->getData();
81-
$page1Data = null;
82-
$page2Data = null;
83-
$page3Data = null;
84-
foreach ($data as $pageData) {
85-
if ($pageData[PageModel::IDENTIFIER] === 'test_custom_layout_page_1') {
86-
$page1Data = $pageData;
87-
} elseif ($pageData[PageModel::IDENTIFIER] === 'test_custom_layout_page_2') {
88-
$page2Data = $pageData;
89-
} elseif ($pageData[PageModel::IDENTIFIER] === 'test_custom_layout_page_3') {
90-
$page3Data = $pageData;
91-
}
92-
}
93-
$this->assertNotEmpty($page1Data);
94-
$this->assertNotEmpty($page2Data);
95-
$this->assertEquals('_existing_', $page1Data['layout_update_selected']);
96-
$this->assertNull($page2Data['layout_update_selected']);
97-
$this->assertEquals('test_selected', $page3Data['layout_update_selected']);
87+
$page = $this->repo->execute($identifier, 0);
88+
89+
$request = $this->objectManager->create(RequestInterface::class);
90+
$request->setParam('page_id', $page->getId());
91+
92+
$provider = $this->objectManager->create(
93+
DataProvider::class,
94+
array_merge($this->providerData, ['request' => $request])
95+
);
96+
97+
$data = $provider->getData();
98+
$pageData = $data[$page->getId()];
99+
100+
$this->assertEquals($layoutUpdateSelected, $pageData['layout_update_selected']);
101+
}
102+
103+
/**
104+
* DataProvider for testCustomLayoutData
105+
*
106+
* @return array
107+
*/
108+
public function customLayoutDataProvider(): array
109+
{
110+
return [
111+
['test_custom_layout_page_1', '_existing_'],
112+
['test_custom_layout_page_2', null],
113+
['test_custom_layout_page_3', 'test_selected'],
114+
];
98115
}
99116

100117
/**

0 commit comments

Comments
 (0)