Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Page.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Cms\Helper;
7 
9 
17 {
21  const XML_PATH_NO_ROUTE_PAGE = 'web/default/cms_no_route';
22 
26  const XML_PATH_NO_COOKIES_PAGE = 'web/default/cms_no_cookies';
27 
31  const XML_PATH_HOME_PAGE = 'web/default/cms_home_page';
32 
38  protected $_design;
39 
43  protected $_page;
44 
48  protected $messageManager;
49 
53  protected $_localeDate;
54 
60  protected $_storeManager;
61 
67  protected $_pageFactory;
68 
72  protected $_escaper;
73 
77  protected $resultPageFactory;
78 
93  public function __construct(
94  \Magento\Framework\App\Helper\Context $context,
95  \Magento\Framework\Message\ManagerInterface $messageManager,
96  \Magento\Cms\Model\Page $page,
97  \Magento\Framework\View\DesignInterface $design,
98  \Magento\Cms\Model\PageFactory $pageFactory,
99  \Magento\Store\Model\StoreManagerInterface $storeManager,
100  \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
101  \Magento\Framework\Escaper $escaper,
102  \Magento\Framework\View\Result\PageFactory $resultPageFactory
103  ) {
104  $this->messageManager = $messageManager;
105  $this->_page = $page;
106  $this->_design = $design;
107  $this->_pageFactory = $pageFactory;
108  $this->_storeManager = $storeManager;
109  $this->_localeDate = $localeDate;
110  $this->_escaper = $escaper;
111  $this->resultPageFactory = $resultPageFactory;
112  parent::__construct($context);
113  }
114 
122  public function prepareResultPage(Action $action, $pageId = null)
123  {
124  if ($pageId !== null && $pageId !== $this->_page->getId()) {
125  $delimiterPosition = strrpos($pageId, '|');
126  if ($delimiterPosition) {
127  $pageId = substr($pageId, 0, $delimiterPosition);
128  }
129 
130  $this->_page->setStoreId($this->_storeManager->getStore()->getId());
131  if (!$this->_page->load($pageId)) {
132  return false;
133  }
134  }
135 
136  if (!$this->_page->getId()) {
137  return false;
138  }
139 
140  $inRange = $this->_localeDate->isScopeDateInInterval(
141  null,
142  $this->_page->getCustomThemeFrom(),
143  $this->_page->getCustomThemeTo()
144  );
145 
146  if ($this->_page->getCustomTheme()) {
147  if ($inRange) {
148  $this->_design->setDesignTheme($this->_page->getCustomTheme());
149  }
150  }
152  $resultPage = $this->resultPageFactory->create();
153  $this->setLayoutType($inRange, $resultPage);
154  $resultPage->addHandle('cms_page_view');
155  $resultPage->addPageLayoutHandles(['id' => str_replace('/', '_', $this->_page->getIdentifier())]);
156 
157  $this->_eventManager->dispatch(
158  'cms_page_render',
159  ['page' => $this->_page, 'controller_action' => $action, 'request' => $this->_getRequest()]
160  );
161 
162  if ($this->_page->getCustomLayoutUpdateXml() && $inRange) {
163  $layoutUpdate = $this->_page->getCustomLayoutUpdateXml();
164  } else {
165  $layoutUpdate = $this->_page->getLayoutUpdateXml();
166  }
167  if (!empty($layoutUpdate)) {
168  $resultPage->getLayout()->getUpdate()->addUpdate($layoutUpdate);
169  }
170 
171  $contentHeadingBlock = $resultPage->getLayout()->getBlock('page_content_heading');
172  if ($contentHeadingBlock) {
173  $contentHeading = $this->_escaper->escapeHtml($this->_page->getContentHeading());
174  $contentHeadingBlock->setContentHeading($contentHeading);
175  }
176 
177  return $resultPage;
178  }
179 
186  public function getPageUrl($pageId = null)
187  {
189  $page = $this->_pageFactory->create();
190  if ($pageId !== null && $pageId !== $page->getId()) {
191  $page->setStoreId($this->_storeManager->getStore()->getId());
192  $page->load($pageId);
193  }
194 
195  if (!$page->getId()) {
196  return null;
197  }
198 
199  return $this->_urlBuilder->getUrl(null, ['_direct' => $page->getIdentifier()]);
200  }
201 
209  protected function setLayoutType($inRange, $resultPage)
210  {
211  if ($this->_page->getPageLayout()) {
212  if ($this->_page->getCustomPageLayout()
213  && $this->_page->getCustomPageLayout() != 'empty'
214  && $inRange
215  ) {
216  $handle = $this->_page->getCustomPageLayout();
217  } else {
218  $handle = $this->_page->getPageLayout();
219  }
220  $resultPage->getConfig()->setPageLayout($handle);
221  }
222  return $resultPage;
223  }
224 }
const XML_PATH_NO_ROUTE_PAGE
Definition: Page.php:21
$storeManager
const XML_PATH_HOME_PAGE
Definition: Page.php:31
$page
Definition: pages.php:8
setLayoutType($inRange, $resultPage)
Definition: Page.php:209
__construct(\Magento\Framework\App\Helper\Context $context, \Magento\Framework\Message\ManagerInterface $messageManager, \Magento\Cms\Model\Page $page, \Magento\Framework\View\DesignInterface $design, \Magento\Cms\Model\PageFactory $pageFactory, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate, \Magento\Framework\Escaper $escaper, \Magento\Framework\View\Result\PageFactory $resultPageFactory)
Definition: Page.php:93
const XML_PATH_NO_COOKIES_PAGE
Definition: Page.php:26
$handle