5
5
*/
6
6
namespace Magento \Cms \Model \Page ;
7
7
8
- use Magento \Cms \Model \Page ;
8
+ use Magento \Cms \Api \Data \PageInterface ;
9
+ use Magento \Cms \Api \PageRepositoryInterface ;
10
+ use Magento \Cms \Model \PageFactory ;
9
11
use Magento \Cms \Model \ResourceModel \Page \CollectionFactory ;
10
12
use Magento \Framework \App \ObjectManager ;
11
13
use Magento \Framework \App \Request \DataPersistorInterface ;
12
14
use Magento \Framework \App \RequestInterface ;
13
- use Magento \Ui \DataProvider \Modifier \PoolInterface ;
14
15
use Magento \Framework \AuthorizationInterface ;
16
+ use Magento \Framework \Exception \LocalizedException ;
17
+ use Magento \Ui \DataProvider \Modifier \PoolInterface ;
18
+ use Magento \Ui \DataProvider \ModifierPoolDataProvider ;
19
+ use Psr \Log \LoggerInterface ;
15
20
16
21
/**
17
- * Class DataProvider
22
+ * Cms Page DataProvider
18
23
*/
19
- class DataProvider extends \ Magento \ Ui \ DataProvider \ ModifierPoolDataProvider
24
+ class DataProvider extends ModifierPoolDataProvider
20
25
{
21
- /**
22
- * @var \Magento\Cms\Model\ResourceModel\Page\Collection
23
- */
24
- protected $ collection ;
25
-
26
26
/**
27
27
* @var DataPersistorInterface
28
28
*/
@@ -33,6 +33,11 @@ class DataProvider extends \Magento\Ui\DataProvider\ModifierPoolDataProvider
33
33
*/
34
34
protected $ loadedData ;
35
35
36
+ /**
37
+ * @var PageRepositoryInterface
38
+ */
39
+ private $ pageRepository ;
40
+
36
41
/**
37
42
* @var AuthorizationInterface
38
43
*/
@@ -49,9 +54,14 @@ class DataProvider extends \Magento\Ui\DataProvider\ModifierPoolDataProvider
49
54
private $ customLayoutManager ;
50
55
51
56
/**
52
- * @var CollectionFactory
57
+ * @var PageFactory
58
+ */
59
+ private $ pageFactory ;
60
+
61
+ /**
62
+ * @var LoggerInterface
53
63
*/
54
- private $ collectionFactory ;
64
+ private $ logger ;
55
65
56
66
/**
57
67
* @param string $name
@@ -65,6 +75,9 @@ class DataProvider extends \Magento\Ui\DataProvider\ModifierPoolDataProvider
65
75
* @param AuthorizationInterface|null $auth
66
76
* @param RequestInterface|null $request
67
77
* @param CustomLayoutManagerInterface|null $customLayoutManager
78
+ * @param PageRepositoryInterface|null $pageRepository
79
+ * @param PageFactory|null $pageFactory
80
+ * @param LoggerInterface|null $logger
68
81
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
69
82
*/
70
83
public function __construct (
@@ -78,33 +91,22 @@ public function __construct(
78
91
PoolInterface $ pool = null ,
79
92
?AuthorizationInterface $ auth = null ,
80
93
?RequestInterface $ request = null ,
81
- ?CustomLayoutManagerInterface $ customLayoutManager = null
94
+ ?CustomLayoutManagerInterface $ customLayoutManager = null ,
95
+ ?PageRepositoryInterface $ pageRepository = null ,
96
+ ?PageFactory $ pageFactory = null ,
97
+ ?LoggerInterface $ logger = null
82
98
) {
99
+ parent ::__construct ($ name , $ primaryFieldName , $ requestFieldName , $ meta , $ data , $ pool );
83
100
$ this ->collection = $ pageCollectionFactory ->create ();
84
- $ this ->collectionFactory = $ pageCollectionFactory ;
85
101
$ this ->dataPersistor = $ dataPersistor ;
86
- parent ::__construct ($ name , $ primaryFieldName , $ requestFieldName , $ meta , $ data , $ pool );
87
102
$ this ->auth = $ auth ?? ObjectManager::getInstance ()->get (AuthorizationInterface::class);
88
103
$ this ->meta = $ this ->prepareMeta ($ this ->meta );
89
104
$ this ->request = $ request ?? ObjectManager::getInstance ()->get (RequestInterface::class);
90
105
$ this ->customLayoutManager = $ customLayoutManager
91
106
?? 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 ;
107
+ $ this ->pageRepository = $ pageRepository ?? ObjectManager::getInstance ()->get (PageRepositoryInterface::class);
108
+ $ this ->pageFactory = $ pageFactory ?: ObjectManager::getInstance ()->get (PageFactory::class);
109
+ $ this ->logger = $ logger ?: ObjectManager::getInstance ()->get (LoggerInterface::class);
108
110
}
109
111
110
112
/**
@@ -128,29 +130,53 @@ public function getData()
128
130
if (isset ($ this ->loadedData )) {
129
131
return $ this ->loadedData ;
130
132
}
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_ ' ;
133
+
134
+ $ page = $ this ->getCurrentPage ();
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
+ }
140
+
141
+ return $ this ->loadedData ;
142
+ }
143
+
144
+ /**
145
+ * Return current page
146
+ *
147
+ * @return PageInterface
148
+ */
149
+ private function getCurrentPage (): PageInterface
150
+ {
151
+ $ pageId = $ this ->getPageId ();
152
+ if ($ pageId ) {
153
+ try {
154
+ $ page = $ this ->pageRepository ->getById ($ pageId );
155
+ } catch (LocalizedException $ exception ) {
156
+ $ page = $ this ->pageFactory ->create ();
139
157
}
158
+
159
+ return $ page ;
140
160
}
141
161
142
162
$ 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 ' );
163
+ if (empty ($ data )) {
164
+ return $ this ->pageFactory ->create ();
151
165
}
166
+ $ this ->dataPersistor ->clear ('cms_page ' );
152
167
153
- return $ this ->loadedData ;
168
+ return $ this ->pageFactory ->create ()
169
+ ->setData ($ data );
170
+ }
171
+
172
+ /**
173
+ * Returns current page id from request
174
+ *
175
+ * @return int
176
+ */
177
+ private function getPageId (): int
178
+ {
179
+ return (int ) $ this ->request ->getParam ($ this ->getRequestFieldName ());
154
180
}
155
181
156
182
/**
@@ -186,16 +212,20 @@ public function getMeta()
186
212
187
213
//List of custom layout files available for current page.
188
214
$ options = [['label ' => 'No update ' , 'value ' => '_no_update_ ' ]];
189
- if ($ page = $ this ->findCurrentPage ()) {
190
- //We must have a specific page selected.
191
- //If custom layout XML is set then displaying this special option.
215
+
216
+ $ page = null ;
217
+ try {
218
+ $ page = $ this ->pageRepository ->getById ($ this ->getPageId ());
192
219
if ($ page ->getCustomLayoutUpdateXml () || $ page ->getLayoutUpdateXml ()) {
193
220
$ options [] = ['label ' => 'Use existing layout update XML ' , 'value ' => '_existing_ ' ];
194
221
}
195
222
foreach ($ this ->customLayoutManager ->fetchAvailableFiles ($ page ) as $ layoutFile ) {
196
223
$ options [] = ['label ' => $ layoutFile , 'value ' => $ layoutFile ];
197
224
}
225
+ } catch (LocalizedException $ e ) {
226
+ $ this ->logger ->error ($ e ->getMessage ());
198
227
}
228
+
199
229
$ customLayoutMeta = [
200
230
'design ' => [
201
231
'children ' => [
0 commit comments