Skip to content

Commit

Permalink
creating new page via factory
Browse files Browse the repository at this point in the history
  • Loading branch information
engcom-Charlie authored Jan 15, 2021
1 parent a2327c9 commit 9ee832e
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions app/code/Magento/Cms/Model/Page/DataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

use Magento\Cms\Api\Data\PageInterface;
use Magento\Cms\Api\PageRepositoryInterface;
use Magento\Cms\Model\Page;
use Magento\Cms\Model\PageFactory;
use Magento\Cms\Model\ResourceModel\Page\CollectionFactory;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\Request\DataPersistorInterface;
Expand Down Expand Up @@ -53,6 +53,11 @@ class DataProvider extends ModifierPoolDataProvider
*/
private $customLayoutManager;

/**
* @var PageFactory
*/
private $pageFactory;

/**
* @var LoggerInterface
*/
Expand All @@ -71,6 +76,7 @@ class DataProvider extends ModifierPoolDataProvider
* @param RequestInterface|null $request
* @param CustomLayoutManagerInterface|null $customLayoutManager
* @param PageRepositoryInterface|null $pageRepository
* @param PageFactory|null $pageFactory
* @param LoggerInterface|null $logger
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
Expand All @@ -87,6 +93,7 @@ public function __construct(
?RequestInterface $request = null,
?CustomLayoutManagerInterface $customLayoutManager = null,
?PageRepositoryInterface $pageRepository = null,
?PageFactory $pageFactory = null,
?LoggerInterface $logger = null
) {
parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data, $pool);
Expand All @@ -98,6 +105,7 @@ public function __construct(
$this->customLayoutManager = $customLayoutManager
?? ObjectManager::getInstance()->get(CustomLayoutManagerInterface::class);
$this->pageRepository = $pageRepository ?? ObjectManager::getInstance()->get(PageRepositoryInterface::class);
$this->pageFactory = $pageFactory ?: ObjectManager::getInstance()->get(PageFactory::class);
$this->logger = $logger ?: ObjectManager::getInstance()->get(LoggerInterface::class);
}

Expand Down Expand Up @@ -140,24 +148,24 @@ public function getData()
*/
private function getCurrentPage(): PageInterface
{
$newPage = $this->collection->getNewEmptyItem();
$pageId = $this->getPageId();
if ($pageId) {
try {
$page = $this->pageRepository->getById($pageId);
} catch (LocalizedException $exception) {
$page = $newPage;
$page = $this->pageFactory->create();
}

return $page;
}

$data = $this->dataPersistor->get('cms_page');
if (empty($data)) {
return $newPage;
return $this->pageFactory->create();
}
$this->dataPersistor->clear('cms_page');
$page = $newPage->setData($data);
$page = $this->pageFactory->create()
->setData($data);

return $page;
}
Expand Down

0 comments on commit 9ee832e

Please sign in to comment.