Skip to content

Commit

Permalink
FIX Perfomance Issue for Backend EDIT of CMS #30936
Browse files Browse the repository at this point in the history
  • Loading branch information
larsroettig committed Nov 17, 2020
1 parent e9fa180 commit 94a7467
Showing 1 changed file with 65 additions and 50 deletions.
115 changes: 65 additions & 50 deletions app/code/Magento/Cms/Model/Page/DataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
*/
namespace Magento\Cms\Model\Page;

use Magento\Cms\Api\PageRepositoryInterface;
use Magento\Cms\Model\Page;
use Magento\Cms\Model\ResourceModel\Page\CollectionFactory;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Ui\DataProvider\Modifier\PoolInterface;
use Magento\Framework\AuthorizationInterface;

Expand All @@ -18,21 +20,16 @@
*/
class DataProvider extends \Magento\Ui\DataProvider\ModifierPoolDataProvider
{
/**
* @var \Magento\Cms\Model\ResourceModel\Page\Collection
*/
protected $collection;

/**
* @var DataPersistorInterface
*/
protected $dataPersistor;

/**
* @var array
*/
protected $loadedData;

/** @var PageRepositoryInterface */
private $pageRepository;
/**
* @var AuthorizationInterface
*/
Expand All @@ -47,11 +44,10 @@ class DataProvider extends \Magento\Ui\DataProvider\ModifierPoolDataProvider
* @var CustomLayoutManagerInterface
*/
private $customLayoutManager;

/**
* @var CollectionFactory
* @var null|array
*/
private $collectionFactory;
private $loadedPages;

/**
* @param string $name
Expand All @@ -65,6 +61,7 @@ class DataProvider extends \Magento\Ui\DataProvider\ModifierPoolDataProvider
* @param AuthorizationInterface|null $auth
* @param RequestInterface|null $request
* @param CustomLayoutManagerInterface|null $customLayoutManager
* @param PageRepositoryInterface|null $pageRepository
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
Expand All @@ -78,33 +75,20 @@ public function __construct(
PoolInterface $pool = null,
?AuthorizationInterface $auth = null,
?RequestInterface $request = null,
?CustomLayoutManagerInterface $customLayoutManager = null
?CustomLayoutManagerInterface $customLayoutManager = null,
?PageRepositoryInterface $pageRepository = null
) {

parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data, $pool);

$this->collection = $pageCollectionFactory->create();
$this->collectionFactory = $pageCollectionFactory;
$this->dataPersistor = $dataPersistor;
parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data, $pool);
$this->auth = $auth ?? ObjectManager::getInstance()->get(AuthorizationInterface::class);
$this->meta = $this->prepareMeta($this->meta);
$this->request = $request ?? ObjectManager::getInstance()->get(RequestInterface::class);
$this->customLayoutManager = $customLayoutManager
?? ObjectManager::getInstance()->get(CustomLayoutManagerInterface::class);
}

/**
* Find requested page.
*
* @return Page|null
*/
private function findCurrentPage(): ?Page
{
if ($this->getRequestFieldName() && ($pageId = (int)$this->request->getParam($this->getRequestFieldName()))) {
//Loading data for the collection.
$this->getData();
return $this->collection->getItemById($pageId);
}

return null;
$this->pageRepository = $pageRepository ?? ObjectManager::getInstance()->get(PageRepositoryInterface::class);
}

/**
Expand All @@ -113,7 +97,7 @@ private function findCurrentPage(): ?Page
* @param array $meta
* @return array
*/
public function prepareMeta(array $meta)
public function prepareMeta(array $meta): array
{
return $meta;
}
Expand All @@ -123,40 +107,71 @@ public function prepareMeta(array $meta)
*
* @return array
*/
public function getData()
public function getData(): array
{
if (isset($this->loadedData)) {
return $this->loadedData;
}
$this->collection = $this->collectionFactory->create();
$items = $this->collection->getItems();
/** @var $page \Magento\Cms\Model\Page */
foreach ($items as $page) {
$this->loadedData[$page->getId()] = $page->getData();
if ($page->getCustomLayoutUpdateXml() || $page->getLayoutUpdateXml()) {
//Deprecated layout update exists.
$this->loadedData[$page->getId()]['layout_update_selected'] = '_existing_';
}

$page = $this->getCurrentPage();
if ($page === null) {
return [];
}

$pageId = $page->getId();
$this->loadedData[$pageId] = $page->getData();
if ($page->getCustomLayoutUpdateXml() || $page->getLayoutUpdateXml()) {
//Deprecated layout update exists.
$this->loadedData[$pageId]['layout_update_selected'] = '_existing_';
}

$data = $this->dataPersistor->get('cms_page');
if (!empty($data)) {
$page = $this->collection->getNewEmptyItem();
$page->setData($data);
$this->loadedData[$page->getId()] = $page->getData();
if ($page->getCustomLayoutUpdateXml() || $page->getLayoutUpdateXml()) {
$this->loadedData[$page->getId()]['layout_update_selected'] = '_existing_';
}
$this->dataPersistor->clear('cms_page');
if (empty($data)) {
return $this->loadedData;
}

$page = $this->collection->getNewEmptyItem();
$page->setData($data);
$this->loadedData[$pageId] = $page->getData();
if ($page->getCustomLayoutUpdateXml() || $page->getLayoutUpdateXml()) {
$this->loadedData[$pageId]['layout_update_selected'] = '_existing_';
}
$this->dataPersistor->clear('cms_page');

return $this->loadedData;
}

/**
* Loads the current page by current request params.
* @return Page|null
*/
public function getCurrentPage(): ?Page
{
if (!$this->getRequestFieldName()) {
return null;
}

$pageId = (int)$this->request->getParam($this->getRequestFieldName());
if ($pageId === 0) {
return null;
}

if (isset($this->loadedPages[$pageId])) {
return $this->loadedPages[$pageId];
}

try {
$this->loadedPages[$pageId] = $this->pageRepository->getById($pageId);
return $this->loadedPages[$pageId];
} catch (LocalizedException $exception) {
return null;
}
}

/**
* @inheritDoc
*/
public function getMeta()
public function getMeta(): array
{
$meta = parent::getMeta();

Expand Down Expand Up @@ -186,7 +201,7 @@ public function getMeta()

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

0 comments on commit 94a7467

Please sign in to comment.