Skip to content

Commit

Permalink
fixed clearing form after saving with error
Browse files Browse the repository at this point in the history
  • Loading branch information
engcom-Charlie committed Jan 13, 2021
1 parent 65da3e6 commit a2327c9
Showing 1 changed file with 34 additions and 26 deletions.
60 changes: 34 additions & 26 deletions app/code/Magento/Cms/Model/Page/DataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
namespace Magento\Cms\Model\Page;

use Magento\Cms\Api\Data\PageInterface;
use Magento\Cms\Api\PageRepositoryInterface;
use Magento\Cms\Model\Page;
use Magento\Cms\Model\ResourceModel\Page\CollectionFactory;
Expand Down Expand Up @@ -122,46 +123,53 @@ public function getData()
return $this->loadedData;
}

try {
$page = $this->getCurrentPage();
} catch (LocalizedException $exception) {
return [];
}

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

$data = $this->dataPersistor->get('cms_page');
if (empty($data)) {
return $this->loadedData;
return $this->loadedData;
}

/**
* Return current page
*
* @return PageInterface
*/
private function getCurrentPage(): PageInterface
{
$newPage = $this->collection->getNewEmptyItem();
$pageId = $this->getPageId();
if ($pageId) {
try {
$page = $this->pageRepository->getById($pageId);
} catch (LocalizedException $exception) {
$page = $newPage;
}

return $page;
}

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

return $this->loadedData;
return $page;
}

/**
* Loads the current page by current request params.
* Returns current page id from request
*
* @return Page
* @throws LocalizedException
* @return int
*/
private function getCurrentPage(): Page
private function getPageId(): int
{
$pageId = $this->request->getParam($this->getRequestFieldName(), 0);

return $this->pageRepository->getById($pageId);
return (int) $this->request->getParam($this->getRequestFieldName());
}

/**
Expand Down Expand Up @@ -200,7 +208,7 @@ public function getMeta()

$page = null;
try {
$page = $this->getCurrentPage();
$page = $this->pageRepository->getById($this->getPageId());
} catch (LocalizedException $e) {
$this->logger->error($e->getMessage());
}
Expand Down

0 comments on commit a2327c9

Please sign in to comment.