10
10
use Magento \Cms \Api \Data \PageInterface ;
11
11
use Magento \Cms \Api \GetPageByIdentifierInterface ;
12
12
use Magento \Cms \Api \PageRepositoryInterface ;
13
+ use Magento \Framework \App \ObjectManager ;
13
14
use Magento \Framework \Exception \NoSuchEntityException ;
14
15
use Magento \Store \Model \StoreManagerInterface ;
15
16
use Magento \Widget \Model \Template \FilterEmulate ;
@@ -40,21 +41,22 @@ class Page
40
41
private $ widgetFilter ;
41
42
42
43
/**
43
- * @param GetPageByIdentifierInterface $getPageByIdentifier
44
- * @param FilterEmulate $widgetFilter
45
44
* @param PageRepositoryInterface $pageRepository
45
+ * @param FilterEmulate $widgetFilter
46
+ * @param GetPageByIdentifierInterface $getPageByIdentifier
46
47
* @param StoreManagerInterface $storeManager
47
48
*/
48
49
public function __construct (
49
- GetPageByIdentifierInterface $ getPageByIdentifier ,
50
- FilterEmulate $ widgetFilter ,
51
50
PageRepositoryInterface $ pageRepository ,
52
- StoreManagerInterface $ storeManager
51
+ FilterEmulate $ widgetFilter ,
52
+ GetPageByIdentifierInterface $ getPageByIdentifier = null ,
53
+ StoreManagerInterface $ storeManager = null
53
54
) {
54
- $ this -> pageByIdentifier = $ getPageByIdentifier ;
55
+
55
56
$ this ->pageRepository = $ pageRepository ;
56
- $ this ->storeManager = $ storeManager ;
57
57
$ this ->widgetFilter = $ widgetFilter ;
58
+ $ this ->pageByIdentifier = $ getPageByIdentifier ?: ObjectManager::getInstance ()->get (GetPageByIdentifierInterface::class);
59
+ $ this ->storeManager = $ storeManager ?: ObjectManager::getInstance ()->get (StoreManagerInterface::class);
58
60
}
59
61
60
62
/**
@@ -75,23 +77,12 @@ public function getData(int $pageId): array
75
77
throw new NoSuchEntityException ();
76
78
}
77
79
78
- $ renderedContent = $ this ->widgetFilter ->filter ($ page ->getContent ());
79
-
80
- $ pageData = [
81
- PageInterface::PAGE_ID => $ page ->getId (),
82
- 'url_key ' => $ page ->getIdentifier (),
83
- PageInterface::TITLE => $ page ->getTitle (),
84
- PageInterface::CONTENT => $ renderedContent ,
85
- PageInterface::CONTENT_HEADING => $ page ->getContentHeading (),
86
- PageInterface::PAGE_LAYOUT => $ page ->getPageLayout (),
87
- PageInterface::META_TITLE => $ page ->getMetaTitle (),
88
- PageInterface::META_DESCRIPTION => $ page ->getMetaDescription (),
89
- PageInterface::META_KEYWORDS => $ page ->getMetaKeywords (),
90
- ];
91
- return $ pageData ;
80
+ return $ this ->convertPageData ($ page );
92
81
}
93
82
94
83
/**
84
+ * Returns page data by page_id
85
+ *
95
86
* @param int $pageId
96
87
* @return array
97
88
* @throws NoSuchEntityException
@@ -100,10 +91,12 @@ public function getDataByPageId(int $pageId): array
100
91
{
101
92
$ page = $ this ->pageRepository ->getById ($ pageId );
102
93
103
- return $ this ->convertPageData ($ page );
94
+ return $ this ->convertPageData ($ page, false , true );
104
95
}
105
96
106
97
/**
98
+ * Returns page data by page identifier
99
+ *
107
100
* @param string $pageIdentifier
108
101
* @return array
109
102
* @throws NoSuchEntityException
@@ -113,15 +106,17 @@ public function getDataByPageIdentifier(string $pageIdentifier): array
113
106
$ storeId = (int )$ this ->storeManager ->getStore ()->getId ();
114
107
$ page = $ this ->pageByIdentifier ->execute ($ pageIdentifier , $ storeId );
115
108
116
- return $ this ->convertPageData ($ page );
109
+ return $ this ->convertPageData ($ page, false , true );
117
110
}
118
111
119
112
/**
120
113
* @param PageInterface $page
114
+ * @param bool $includePageId
115
+ * @param bool $includePageIdentifier
121
116
* @return array
122
117
* @throws NoSuchEntityException
123
118
*/
124
- private function convertPageData (PageInterface $ page )
119
+ private function convertPageData (PageInterface $ page, $ includePageId = true , $ includePageIdentifier = false )
125
120
{
126
121
if (false === $ page ->isActive ()) {
127
122
throw new NoSuchEntityException ();
@@ -131,7 +126,6 @@ private function convertPageData(PageInterface $page)
131
126
132
127
$ pageData = [
133
128
'url_key ' => $ page ->getIdentifier (),
134
- PageInterface::IDENTIFIER => $ page ->getIdentifier (),
135
129
PageInterface::TITLE => $ page ->getTitle (),
136
130
PageInterface::CONTENT => $ renderedContent ,
137
131
PageInterface::CONTENT_HEADING => $ page ->getContentHeading (),
@@ -140,6 +134,15 @@ private function convertPageData(PageInterface $page)
140
134
PageInterface::META_DESCRIPTION => $ page ->getMetaDescription (),
141
135
PageInterface::META_KEYWORDS => $ page ->getMetaKeywords (),
142
136
];
137
+
138
+ if ($ includePageId ) {
139
+ $ pageData [PageInterface::PAGE_ID ] = $ page ->getId ();
140
+ }
141
+
142
+ if ($ includePageIdentifier ) {
143
+ $ pageData [PageInterface::IDENTIFIER ] = $ page ->getIdentifier ();
144
+ }
145
+
143
146
return $ pageData ;
144
147
}
145
148
}
0 commit comments