Skip to content

Commit 2dbaca3

Browse files
committed
Fixed issue with Data Persistor
1 parent 33a2b5c commit 2dbaca3

File tree

5 files changed

+107
-45
lines changed

5 files changed

+107
-45
lines changed

Controller/Adminhtml/Actions.php

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
namespace Magefan\Blog\Controller\Adminhtml;
1010

11+
use Magento\Framework\App\Request\DataPersistorInterface;
12+
use Magento\Backend\App\Action\Context;
13+
1114
/**
1215
* Abstract admin controller
1316
*/
@@ -74,6 +77,25 @@ abstract class Actions extends \Magento\Backend\App\Action
7477
*/
7578
protected $_coreRegistry = null;
7679

80+
81+
/**
82+
* @var DataPersistorInterface
83+
*/
84+
protected $dataPersistor;
85+
86+
/**
87+
* @param Action\Context $context
88+
* @param PostDataProcessor $dataProcessor
89+
* @param DataPersistorInterface $dataPersistor
90+
*/
91+
public function __construct(
92+
Context $context,
93+
DataPersistorInterface $dataPersistor
94+
) {
95+
$this->dataPersistor = $dataPersistor;
96+
parent::__construct($context);
97+
}
98+
7799
/**
78100
* Action execute
79101
* @return \Magento\Framework\Controller\ResultInterface
@@ -195,6 +217,8 @@ public function _saveAction()
195217

196218
try {
197219
$params = $this->_paramsHolder ? $request->getParam($this->_paramsHolder) : $request->getParams();
220+
$params = $this->filterParams($params);
221+
198222
$idFieldName = $model->getResource()->getIdFieldName();
199223
if (isset($params[$idFieldName]) && empty($params[$idFieldName])) {
200224
unset($params[$idFieldName]);
@@ -209,7 +233,7 @@ public function _saveAction()
209233
$this->_setFormData(false);
210234
} catch (\Magento\Framework\Exception\LocalizedException $e) {
211235
$this->messageManager->addError(nl2br($e->getMessage()));
212-
$this->_setFormData();
236+
$this->_setFormData($params);
213237
} catch (\Exception $e) {
214238
$this->messageManager->addException(
215239
$e,
@@ -218,7 +242,7 @@ public function _saveAction()
218242
$e->getMessage()
219243
)
220244
);
221-
$this->_setFormData();
245+
$this->_setFormData($params);
222246
}
223247

224248
$hasError = (bool)$this->messageManager->getMessages()->getCountByType(
@@ -413,12 +437,32 @@ protected function _configAction()
413437
*/
414438
protected function _setFormData($data = null)
415439
{
416-
$this->_getSession()->setData($this->_formSessionKey,
417-
is_null($data) ? $this->getRequest()->getParams() : $data);
440+
if (null === $data) {
441+
$data = $this->getRequest()->getParams();
442+
}
443+
444+
if (false === $data) {
445+
$this->dataPersistor->clear($this->_formSessionKey);
446+
} else {
447+
$this->dataPersistor->set($this->_formSessionKey, $data);
448+
}
449+
450+
/* deprecated save in session */
451+
$this->_getSession()->setData($this->_formSessionKey, $data);
418452

419453
return $this;
420454
}
421455

456+
/**
457+
* Filter request params
458+
* @param array $data
459+
* @return array
460+
*/
461+
protected function filterParams($data)
462+
{
463+
return $data;
464+
}
465+
422466
/**
423467
* Get core registry
424468
* @return void

Controller/Adminhtml/Category/Save.php

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,30 @@
1414
class Save extends \Magefan\Blog\Controller\Adminhtml\Category
1515
{
1616
/**
17-
* Before model save
17+
* After model save
1818
* @param \Magefan\Blog\Model\Category $model
1919
* @param \Magento\Framework\App\Request\Http $request
2020
* @return void
2121
*/
22-
protected function _beforeSave($model, $request)
22+
protected function _afterSave($model, $request)
23+
{
24+
$model->addData(
25+
[
26+
'parent_id' => $model->getParentId(),
27+
'level' => $model->getLevel(),
28+
]
29+
);
30+
}
31+
32+
/**
33+
* Filter request params
34+
* @param array $data
35+
* @return array
36+
*/
37+
protected function filterParams($data)
2338
{
2439
/* Prepare dates */
2540
$dateFilter = $this->_objectManager->create('Magento\Framework\Stdlib\DateTime\Filter\Date');
26-
$data = $model->getData();
2741

2842
$filterRules = [];
2943
foreach (['custom_theme_from', 'custom_theme_to'] as $dateField) {
@@ -37,23 +51,9 @@ protected function _beforeSave($model, $request)
3751
[],
3852
$data
3953
);
54+
4055
$data = $inputFilter->getUnescaped();
41-
$model->setData($data);
42-
}
43-
44-
/**
45-
* After model save
46-
* @param \Magefan\Blog\Model\Category $model
47-
* @param \Magento\Framework\App\Request\Http $request
48-
* @return void
49-
*/
50-
protected function _afterSave($model, $request)
51-
{
52-
$model->addData(
53-
[
54-
'parent_id' => $model->getParentId(),
55-
'level' => $model->getLevel(),
56-
]
57-
);
56+
57+
return $data;
5858
}
5959
}

Controller/Adminhtml/Post/Save.php

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,6 @@ class Save extends \Magefan\Blog\Controller\Adminhtml\Post
2222
*/
2323
protected function _beforeSave($model, $request)
2424
{
25-
/* Prepare dates */
26-
$dateFilter = $this->_objectManager->create('Magento\Framework\Stdlib\DateTime\Filter\Date');
27-
$data = $model->getData();
28-
29-
$filterRules = [];
30-
foreach (['publish_time', 'custom_theme_from', 'custom_theme_to'] as $dateField) {
31-
if (!empty($data[$dateField])) {
32-
$filterRules[$dateField] = $dateFilter;
33-
}
34-
}
35-
36-
$inputFilter = new \Zend_Filter_Input(
37-
$filterRules,
38-
[],
39-
$data
40-
);
41-
$data = $inputFilter->getUnescaped();
42-
$model->setData($data);
43-
4425
/* Prepare author */
4526
if (!$model->getAuthorId()) {
4627
$authSession = $this->_objectManager->get('Magento\Backend\Model\Auth\Session');
@@ -120,4 +101,33 @@ protected function _beforeSave($model, $request)
120101
}
121102
}
122103

104+
/**
105+
* Filter request params
106+
* @param array $data
107+
* @return array
108+
*/
109+
protected function filterParams($data)
110+
{
111+
/* Prepare dates */
112+
$dateFilter = $this->_objectManager->create('Magento\Framework\Stdlib\DateTime\Filter\Date');
113+
114+
$filterRules = [];
115+
foreach (['publish_time', 'custom_theme_from', 'custom_theme_to'] as $dateField) {
116+
if (!empty($data[$dateField])) {
117+
$filterRules[$dateField] = $dateFilter;
118+
}
119+
}
120+
121+
$inputFilter = new \Zend_Filter_Input(
122+
$filterRules,
123+
[],
124+
$data
125+
);
126+
127+
$data = $inputFilter->getUnescaped();
128+
129+
return $data;
130+
}
131+
132+
123133
}

Ui/DataProvider/Category/Form/CategoryDataProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,12 @@ public function getData()
8282
$this->loadedData[$category->getId()] = $category->getData();
8383
}
8484

85-
$data = $this->dataPersistor->get('current_model');
85+
$data = $this->dataPersistor->get('blog_category_form_data');
8686
if (!empty($data)) {
8787
$category = $this->collection->getNewEmptyItem();
8888
$category->setData($data);
8989
$this->loadedData[$category->getId()] = $category->getData();
90-
$this->dataPersistor->clear('current_model');
90+
$this->dataPersistor->clear('blog_category_form_data');
9191
}
9292

9393
return $this->loadedData;

Ui/DataProvider/Post/Form/PostDataProvider.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,14 @@ public function getData()
125125
$this->loadedData[$post->getId()] = $data;
126126
}
127127

128+
$data = $this->dataPersistor->get('blog_post_form_data');
129+
if (!empty($data)) {
130+
$post = $this->collection->getNewEmptyItem();
131+
$post->setData($data);
132+
$this->loadedData[$post->getId()] = $post->getData();
133+
$this->dataPersistor->clear('blog_post_form_data');
134+
}
135+
128136
return $this->loadedData;
129137
}
130138
}

0 commit comments

Comments
 (0)