55 */
66namespace Magento \Cms \Model \Page ;
77
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 ;
911use Magento \Cms \Model \ResourceModel \Page \CollectionFactory ;
1012use Magento \Framework \App \ObjectManager ;
1113use Magento \Framework \App \Request \DataPersistorInterface ;
1214use Magento \Framework \App \RequestInterface ;
13- use Magento \Ui \DataProvider \Modifier \PoolInterface ;
1415use 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 ;
1520
1621/**
17- * Class DataProvider
22+ * Cms Page DataProvider
1823 */
19- class DataProvider extends \ Magento \ Ui \ DataProvider \ ModifierPoolDataProvider
24+ class DataProvider extends ModifierPoolDataProvider
2025{
21- /**
22- * @var \Magento\Cms\Model\ResourceModel\Page\Collection
23- */
24- protected $ collection ;
25-
2626 /**
2727 * @var DataPersistorInterface
2828 */
@@ -33,6 +33,11 @@ class DataProvider extends \Magento\Ui\DataProvider\ModifierPoolDataProvider
3333 */
3434 protected $ loadedData ;
3535
36+ /**
37+ * @var PageRepositoryInterface
38+ */
39+ private $ pageRepository ;
40+
3641 /**
3742 * @var AuthorizationInterface
3843 */
@@ -49,9 +54,14 @@ class DataProvider extends \Magento\Ui\DataProvider\ModifierPoolDataProvider
4954 private $ customLayoutManager ;
5055
5156 /**
52- * @var CollectionFactory
57+ * @var PageFactory
58+ */
59+ private $ pageFactory ;
60+
61+ /**
62+ * @var LoggerInterface
5363 */
54- private $ collectionFactory ;
64+ private $ logger ;
5565
5666 /**
5767 * @param string $name
@@ -65,6 +75,9 @@ class DataProvider extends \Magento\Ui\DataProvider\ModifierPoolDataProvider
6575 * @param AuthorizationInterface|null $auth
6676 * @param RequestInterface|null $request
6777 * @param CustomLayoutManagerInterface|null $customLayoutManager
78+ * @param PageRepositoryInterface|null $pageRepository
79+ * @param PageFactory|null $pageFactory
80+ * @param LoggerInterface|null $logger
6881 * @SuppressWarnings(PHPMD.ExcessiveParameterList)
6982 */
7083 public function __construct (
@@ -78,33 +91,22 @@ public function __construct(
7891 PoolInterface $ pool = null ,
7992 ?AuthorizationInterface $ auth = null ,
8093 ?RequestInterface $ request = null ,
81- ?CustomLayoutManagerInterface $ customLayoutManager = null
94+ ?CustomLayoutManagerInterface $ customLayoutManager = null ,
95+ ?PageRepositoryInterface $ pageRepository = null ,
96+ ?PageFactory $ pageFactory = null ,
97+ ?LoggerInterface $ logger = null
8298 ) {
99+ parent ::__construct ($ name , $ primaryFieldName , $ requestFieldName , $ meta , $ data , $ pool );
83100 $ this ->collection = $ pageCollectionFactory ->create ();
84- $ this ->collectionFactory = $ pageCollectionFactory ;
85101 $ this ->dataPersistor = $ dataPersistor ;
86- parent ::__construct ($ name , $ primaryFieldName , $ requestFieldName , $ meta , $ data , $ pool );
87102 $ this ->auth = $ auth ?? ObjectManager::getInstance ()->get (AuthorizationInterface::class);
88103 $ this ->meta = $ this ->prepareMeta ($ this ->meta );
89104 $ this ->request = $ request ?? ObjectManager::getInstance ()->get (RequestInterface::class);
90105 $ this ->customLayoutManager = $ customLayoutManager
91106 ?? 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);
108110 }
109111
110112 /**
@@ -128,29 +130,53 @@ public function getData()
128130 if (isset ($ this ->loadedData )) {
129131 return $ this ->loadedData ;
130132 }
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 ();
139157 }
158+
159+ return $ page ;
140160 }
141161
142162 $ 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 ();
151165 }
166+ $ this ->dataPersistor ->clear ('cms_page ' );
152167
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 ());
154180 }
155181
156182 /**
@@ -186,16 +212,20 @@ public function getMeta()
186212
187213 //List of custom layout files available for current page.
188214 $ 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 ());
192219 if ($ page ->getCustomLayoutUpdateXml () || $ page ->getLayoutUpdateXml ()) {
193220 $ options [] = ['label ' => 'Use existing layout update XML ' , 'value ' => '_existing_ ' ];
194221 }
195222 foreach ($ this ->customLayoutManager ->fetchAvailableFiles ($ page ) as $ layoutFile ) {
196223 $ options [] = ['label ' => $ layoutFile , 'value ' => $ layoutFile ];
197224 }
225+ } catch (LocalizedException $ e ) {
226+ $ this ->logger ->error ($ e ->getMessage ());
198227 }
228+
199229 $ customLayoutMeta = [
200230 'design ' => [
201231 'children ' => [
0 commit comments