Skip to content

Commit 94a7467

Browse files
author
larsroettig
committed
FIX Perfomance Issue for Backend EDIT of CMS #30936
1 parent e9fa180 commit 94a7467

File tree

1 file changed

+65
-50
lines changed

1 file changed

+65
-50
lines changed

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

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

8+
use Magento\Cms\Api\PageRepositoryInterface;
89
use Magento\Cms\Model\Page;
910
use Magento\Cms\Model\ResourceModel\Page\CollectionFactory;
1011
use Magento\Framework\App\ObjectManager;
1112
use Magento\Framework\App\Request\DataPersistorInterface;
1213
use Magento\Framework\App\RequestInterface;
14+
use Magento\Framework\Exception\LocalizedException;
1315
use Magento\Ui\DataProvider\Modifier\PoolInterface;
1416
use Magento\Framework\AuthorizationInterface;
1517

@@ -18,21 +20,16 @@
1820
*/
1921
class DataProvider extends \Magento\Ui\DataProvider\ModifierPoolDataProvider
2022
{
21-
/**
22-
* @var \Magento\Cms\Model\ResourceModel\Page\Collection
23-
*/
24-
protected $collection;
25-
2623
/**
2724
* @var DataPersistorInterface
2825
*/
2926
protected $dataPersistor;
30-
3127
/**
3228
* @var array
3329
*/
3430
protected $loadedData;
35-
31+
/** @var PageRepositoryInterface */
32+
private $pageRepository;
3633
/**
3734
* @var AuthorizationInterface
3835
*/
@@ -47,11 +44,10 @@ class DataProvider extends \Magento\Ui\DataProvider\ModifierPoolDataProvider
4744
* @var CustomLayoutManagerInterface
4845
*/
4946
private $customLayoutManager;
50-
5147
/**
52-
* @var CollectionFactory
48+
* @var null|array
5349
*/
54-
private $collectionFactory;
50+
private $loadedPages;
5551

5652
/**
5753
* @param string $name
@@ -65,6 +61,7 @@ class DataProvider extends \Magento\Ui\DataProvider\ModifierPoolDataProvider
6561
* @param AuthorizationInterface|null $auth
6662
* @param RequestInterface|null $request
6763
* @param CustomLayoutManagerInterface|null $customLayoutManager
64+
* @param PageRepositoryInterface|null $pageRepository
6865
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
6966
*/
7067
public function __construct(
@@ -78,33 +75,20 @@ public function __construct(
7875
PoolInterface $pool = null,
7976
?AuthorizationInterface $auth = null,
8077
?RequestInterface $request = null,
81-
?CustomLayoutManagerInterface $customLayoutManager = null
78+
?CustomLayoutManagerInterface $customLayoutManager = null,
79+
?PageRepositoryInterface $pageRepository = null
8280
) {
81+
82+
parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data, $pool);
83+
8384
$this->collection = $pageCollectionFactory->create();
84-
$this->collectionFactory = $pageCollectionFactory;
8585
$this->dataPersistor = $dataPersistor;
86-
parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data, $pool);
8786
$this->auth = $auth ?? ObjectManager::getInstance()->get(AuthorizationInterface::class);
8887
$this->meta = $this->prepareMeta($this->meta);
8988
$this->request = $request ?? ObjectManager::getInstance()->get(RequestInterface::class);
9089
$this->customLayoutManager = $customLayoutManager
9190
?? 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;
91+
$this->pageRepository = $pageRepository ?? ObjectManager::getInstance()->get(PageRepositoryInterface::class);
10892
}
10993

11094
/**
@@ -113,7 +97,7 @@ private function findCurrentPage(): ?Page
11397
* @param array $meta
11498
* @return array
11599
*/
116-
public function prepareMeta(array $meta)
100+
public function prepareMeta(array $meta): array
117101
{
118102
return $meta;
119103
}
@@ -123,40 +107,71 @@ public function prepareMeta(array $meta)
123107
*
124108
* @return array
125109
*/
126-
public function getData()
110+
public function getData(): array
127111
{
128112
if (isset($this->loadedData)) {
129113
return $this->loadedData;
130114
}
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_';
139-
}
115+
116+
$page = $this->getCurrentPage();
117+
if ($page === null) {
118+
return [];
119+
}
120+
121+
$pageId = $page->getId();
122+
$this->loadedData[$pageId] = $page->getData();
123+
if ($page->getCustomLayoutUpdateXml() || $page->getLayoutUpdateXml()) {
124+
//Deprecated layout update exists.
125+
$this->loadedData[$pageId]['layout_update_selected'] = '_existing_';
140126
}
141127

142128
$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');
129+
if (empty($data)) {
130+
return $this->loadedData;
151131
}
152132

133+
$page = $this->collection->getNewEmptyItem();
134+
$page->setData($data);
135+
$this->loadedData[$pageId] = $page->getData();
136+
if ($page->getCustomLayoutUpdateXml() || $page->getLayoutUpdateXml()) {
137+
$this->loadedData[$pageId]['layout_update_selected'] = '_existing_';
138+
}
139+
$this->dataPersistor->clear('cms_page');
140+
153141
return $this->loadedData;
154142
}
155143

144+
/**
145+
* Loads the current page by current request params.
146+
* @return Page|null
147+
*/
148+
public function getCurrentPage(): ?Page
149+
{
150+
if (!$this->getRequestFieldName()) {
151+
return null;
152+
}
153+
154+
$pageId = (int)$this->request->getParam($this->getRequestFieldName());
155+
if ($pageId === 0) {
156+
return null;
157+
}
158+
159+
if (isset($this->loadedPages[$pageId])) {
160+
return $this->loadedPages[$pageId];
161+
}
162+
163+
try {
164+
$this->loadedPages[$pageId] = $this->pageRepository->getById($pageId);
165+
return $this->loadedPages[$pageId];
166+
} catch (LocalizedException $exception) {
167+
return null;
168+
}
169+
}
170+
156171
/**
157172
* @inheritDoc
158173
*/
159-
public function getMeta()
174+
public function getMeta(): array
160175
{
161176
$meta = parent::getMeta();
162177

@@ -186,7 +201,7 @@ public function getMeta()
186201

187202
//List of custom layout files available for current page.
188203
$options = [['label' => 'No update', 'value' => '_no_update_']];
189-
if ($page = $this->findCurrentPage()) {
204+
if ($page = $this->getCurrentPage()) {
190205
//We must have a specific page selected.
191206
//If custom layout XML is set then displaying this special option.
192207
if ($page->getCustomLayoutUpdateXml() || $page->getLayoutUpdateXml()) {

0 commit comments

Comments
 (0)