5
5
*/
6
6
namespace Magento \Cms \Model \Page ;
7
7
8
+ use Magento \Cms \Api \PageRepositoryInterface ;
8
9
use Magento \Cms \Model \Page ;
9
10
use Magento \Cms \Model \ResourceModel \Page \CollectionFactory ;
10
11
use Magento \Framework \App \ObjectManager ;
11
12
use Magento \Framework \App \Request \DataPersistorInterface ;
12
13
use Magento \Framework \App \RequestInterface ;
14
+ use Magento \Framework \Exception \LocalizedException ;
13
15
use Magento \Ui \DataProvider \Modifier \PoolInterface ;
14
16
use Magento \Framework \AuthorizationInterface ;
15
17
18
20
*/
19
21
class DataProvider extends \Magento \Ui \DataProvider \ModifierPoolDataProvider
20
22
{
21
- /**
22
- * @var \Magento\Cms\Model\ResourceModel\Page\Collection
23
- */
24
- protected $ collection ;
25
-
26
23
/**
27
24
* @var DataPersistorInterface
28
25
*/
29
26
protected $ dataPersistor ;
30
-
31
27
/**
32
28
* @var array
33
29
*/
34
30
protected $ loadedData ;
35
-
31
+ /** @var PageRepositoryInterface */
32
+ private $ pageRepository ;
36
33
/**
37
34
* @var AuthorizationInterface
38
35
*/
@@ -47,11 +44,10 @@ class DataProvider extends \Magento\Ui\DataProvider\ModifierPoolDataProvider
47
44
* @var CustomLayoutManagerInterface
48
45
*/
49
46
private $ customLayoutManager ;
50
-
51
47
/**
52
- * @var CollectionFactory
48
+ * @var null|array
53
49
*/
54
- private $ collectionFactory ;
50
+ private $ loadedPages ;
55
51
56
52
/**
57
53
* @param string $name
@@ -65,6 +61,7 @@ class DataProvider extends \Magento\Ui\DataProvider\ModifierPoolDataProvider
65
61
* @param AuthorizationInterface|null $auth
66
62
* @param RequestInterface|null $request
67
63
* @param CustomLayoutManagerInterface|null $customLayoutManager
64
+ * @param PageRepositoryInterface|null $pageRepository
68
65
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
69
66
*/
70
67
public function __construct (
@@ -78,33 +75,20 @@ public function __construct(
78
75
PoolInterface $ pool = null ,
79
76
?AuthorizationInterface $ auth = null ,
80
77
?RequestInterface $ request = null ,
81
- ?CustomLayoutManagerInterface $ customLayoutManager = null
78
+ ?CustomLayoutManagerInterface $ customLayoutManager = null ,
79
+ ?PageRepositoryInterface $ pageRepository = null
82
80
) {
81
+
82
+ parent ::__construct ($ name , $ primaryFieldName , $ requestFieldName , $ meta , $ data , $ pool );
83
+
83
84
$ this ->collection = $ pageCollectionFactory ->create ();
84
- $ this ->collectionFactory = $ pageCollectionFactory ;
85
85
$ this ->dataPersistor = $ dataPersistor ;
86
- parent ::__construct ($ name , $ primaryFieldName , $ requestFieldName , $ meta , $ data , $ pool );
87
86
$ this ->auth = $ auth ?? ObjectManager::getInstance ()->get (AuthorizationInterface::class);
88
87
$ this ->meta = $ this ->prepareMeta ($ this ->meta );
89
88
$ this ->request = $ request ?? ObjectManager::getInstance ()->get (RequestInterface::class);
90
89
$ this ->customLayoutManager = $ customLayoutManager
91
90
?? 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 ;
91
+ $ this ->pageRepository = $ pageRepository ?? ObjectManager::getInstance ()->get (PageRepositoryInterface::class);
108
92
}
109
93
110
94
/**
@@ -113,7 +97,7 @@ private function findCurrentPage(): ?Page
113
97
* @param array $meta
114
98
* @return array
115
99
*/
116
- public function prepareMeta (array $ meta )
100
+ public function prepareMeta (array $ meta ): array
117
101
{
118
102
return $ meta ;
119
103
}
@@ -123,40 +107,71 @@ public function prepareMeta(array $meta)
123
107
*
124
108
* @return array
125
109
*/
126
- public function getData ()
110
+ public function getData (): array
127
111
{
128
112
if (isset ($ this ->loadedData )) {
129
113
return $ this ->loadedData ;
130
114
}
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_ ' ;
139
- }
115
+
116
+ $ page = $ this ->getCurrentPage ();
117
+ if ($ page === null ) {
118
+ return [];
119
+ }
120
+
121
+ $ pageId = $ page ->getId ();
122
+ $ this ->loadedData [$ pageId ] = $ page ->getData ();
123
+ if ($ page ->getCustomLayoutUpdateXml () || $ page ->getLayoutUpdateXml ()) {
124
+ //Deprecated layout update exists.
125
+ $ this ->loadedData [$ pageId ]['layout_update_selected ' ] = '_existing_ ' ;
140
126
}
141
127
142
128
$ 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 ' );
129
+ if (empty ($ data )) {
130
+ return $ this ->loadedData ;
151
131
}
152
132
133
+ $ page = $ this ->collection ->getNewEmptyItem ();
134
+ $ page ->setData ($ data );
135
+ $ this ->loadedData [$ pageId ] = $ page ->getData ();
136
+ if ($ page ->getCustomLayoutUpdateXml () || $ page ->getLayoutUpdateXml ()) {
137
+ $ this ->loadedData [$ pageId ]['layout_update_selected ' ] = '_existing_ ' ;
138
+ }
139
+ $ this ->dataPersistor ->clear ('cms_page ' );
140
+
153
141
return $ this ->loadedData ;
154
142
}
155
143
144
+ /**
145
+ * Loads the current page by current request params.
146
+ * @return Page|null
147
+ */
148
+ public function getCurrentPage (): ?Page
149
+ {
150
+ if (!$ this ->getRequestFieldName ()) {
151
+ return null ;
152
+ }
153
+
154
+ $ pageId = (int )$ this ->request ->getParam ($ this ->getRequestFieldName ());
155
+ if ($ pageId === 0 ) {
156
+ return null ;
157
+ }
158
+
159
+ if (isset ($ this ->loadedPages [$ pageId ])) {
160
+ return $ this ->loadedPages [$ pageId ];
161
+ }
162
+
163
+ try {
164
+ $ this ->loadedPages [$ pageId ] = $ this ->pageRepository ->getById ($ pageId );
165
+ return $ this ->loadedPages [$ pageId ];
166
+ } catch (LocalizedException $ exception ) {
167
+ return null ;
168
+ }
169
+ }
170
+
156
171
/**
157
172
* @inheritDoc
158
173
*/
159
- public function getMeta ()
174
+ public function getMeta (): array
160
175
{
161
176
$ meta = parent ::getMeta ();
162
177
@@ -186,7 +201,7 @@ public function getMeta()
186
201
187
202
//List of custom layout files available for current page.
188
203
$ options = [['label ' => 'No update ' , 'value ' => '_no_update_ ' ]];
189
- if ($ page = $ this ->findCurrentPage ()) {
204
+ if ($ page = $ this ->getCurrentPage ()) {
190
205
//We must have a specific page selected.
191
206
//If custom layout XML is set then displaying this special option.
192
207
if ($ page ->getCustomLayoutUpdateXml () || $ page ->getLayoutUpdateXml ()) {
0 commit comments