forked from algolia/algoliasearch-magento-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLandingPage.php
140 lines (120 loc) · 4 KB
/
LandingPage.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
namespace Algolia\AlgoliaSearch\Block;
use Algolia\AlgoliaSearch\Model\LandingPage as LandingPageModel;
use Algolia\AlgoliaSearch\Model\LandingPageFactory;
use Magento\Catalog\Model\Layer\Resolver as LayerResolver;
use Magento\CatalogSearch\Block\Result;
use Magento\CatalogSearch\Helper\Data;
use Magento\Cms\Model\Template\FilterProvider;
use Magento\Search\Model\QueryFactory;
/**
* @method int getPageId()
*/
class LandingPage extends Result
{
/** @var FilterProvider */
protected $filterProvider;
/** @var LandingPageModel */
protected $landingPage;
/** @var LandingPageFactory */
protected $landingPageFactory;
/**
* Construct
*
* @param Magento\Framework\View\Element\Template\Context $context
* @param LayerResolver $layerResolver
* @param Data $catalogSearchData
* @param QueryFactory $queryFactory
* @param FilterProvider $filterProvider
* @param LandingPageModel $landingPage
* @param LandingPageFactory $landingPageFactory
* @param array $data
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
LayerResolver $layerResolver,
Data $catalogSearchData,
QueryFactory $queryFactory,
FilterProvider $filterProvider,
LandingPageModel $landingPage,
LandingPageFactory $landingPageFactory,
array $data = []
) {
parent::__construct(
$context,
$layerResolver,
$catalogSearchData,
$queryFactory,
$data
);
$this->filterProvider = $filterProvider;
$this->landingPage = $landingPage;
$this->landingPageFactory = $landingPageFactory;
}
/**
* Retrieve Page instance
*
* @return LandingPageModel
*/
public function getPage()
{
if (!$this->hasData('page')) {
if ($this->getPageId()) {
/** @var LandingPageModel $page */
$page = $this->landingPageFactory->create();
$page->setStoreId($this->_storeManager->getStore()->getId())->load($this->getPageId(), 'url_key');
} else {
$page = $this->landingPage;
}
$this->setData('page', $page);
}
return $this->getData('page');
}
/**
* Prepare global layout
*
* @return $this
*/
protected function _prepareLayout()
{
$page = $this->getPage();
$this->pageConfig->addBodyClass('algolia-landingpage-' . $page->getUrlKey());
$metaTitle = $page->getMetaTitle();
$this->pageConfig->getTitle()->set($page->getTitle() ? $page->getTitle() : $metaTitle);
$this->pageConfig->setKeywords($page->getMetaKeywords());
$this->pageConfig->setDescription($page->getMetaDescription());
$this->getLayout()->getBlock('landing_page_content')->setText($this->getLandingPageContent());
$this->getLayout()->getBlock('landing_page_custom_js')->setText($this->getLandingCustomJs());
$this->getLayout()->getBlock('landing_page_custom_css')->setText($this->getLandingCustomCss());
return $this;
}
protected function getLandingPageContent()
{
return $this->filterProvider->getPageFilter()->filter($this->getPage()->getContent());
}
protected function getLandingCustomJs()
{
$customJs = $this->getPage()->getCustomJs();
if (!$customJs) {
return '';
}
return '<script type="text/javascript">' . $customJs . '</script>';
}
protected function getLandingCustomCss()
{
$customCss = $this->getPage()->getCustomCss();
if (!$customCss) {
return '';
}
return '<style type="text/css">' . $customCss . '</style>';
}
/**
* Return identifiers for produced content
*
* @return array
*/
public function getIdentities()
{
return [\Algolia\AlgoliaSearch\Model\LandingPage::CACHE_TAG . '_' . $this->getPage()->getId()];
}
}