forked from tomispepe/magento2-1
/
Result.php
executable file
·231 lines (209 loc) · 5.45 KB
/
Result.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\CatalogSearch\Block;
use Magento\Catalog\Block\Product\ListProduct;
use Magento\Catalog\Model\Layer\Resolver as LayerResolver;
use Magento\CatalogSearch\Helper\Data;
use Magento\CatalogSearch\Model\Resource\Fulltext\Collection;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
use Magento\Search\Model\QueryFactory;
/**
* Product search result block
*/
class Result extends Template
{
/**
* Catalog Product collection
*
* @var Collection
*/
protected $productCollection;
/**
* Catalog search data
*
* @var Data
*/
protected $catalogSearchData;
/**
* Catalog layer
*
* @var \Magento\Catalog\Model\Layer
*/
protected $catalogLayer;
/**
* @var QueryFactory
*/
private $queryFactory;
/**
* @param Context $context
* @param LayerResolver $layerResolver
* @param Data $catalogSearchData
* @param QueryFactory $queryFactory
* @param array $data
*/
public function __construct(
Context $context,
LayerResolver $layerResolver,
Data $catalogSearchData,
QueryFactory $queryFactory,
array $data = []
) {
$this->catalogLayer = $layerResolver->get();
$this->catalogSearchData = $catalogSearchData;
$this->queryFactory = $queryFactory;
parent::__construct($context, $data);
}
/**
* Retrieve query model object
*
* @return \Magento\Search\Model\Query
*/
protected function _getQuery()
{
return $this->queryFactory->get();
}
/**
* Prepare layout
*
* @return $this
*/
protected function _prepareLayout()
{
$title = $this->getSearchQueryText();
$this->pageConfig->getTitle()->set($title);
// add Home breadcrumb
$breadcrumbs = $this->getLayout()->getBlock('breadcrumbs');
if ($breadcrumbs) {
$breadcrumbs->addCrumb(
'home',
[
'label' => __('Home'),
'title' => __('Go to Home Page'),
'link' => $this->_storeManager->getStore()->getBaseUrl()
]
)->addCrumb(
'search',
['label' => $title, 'title' => $title]
);
}
return parent::_prepareLayout();
}
/**
* Retrieve additional blocks html
*
* @return string
*/
public function getAdditionalHtml()
{
return $this->getLayout()->getBlock('search_result_list')->getChildHtml('additional');
}
/**
* Retrieve search list toolbar block
*
* @return ListProduct
*/
public function getListBlock()
{
return $this->getChildBlock('search_result_list');
}
/**
* Set search available list orders
*
* @return $this
*/
public function setListOrders()
{
$category = $this->catalogLayer->getCurrentCategory();
/* @var $category \Magento\Catalog\Model\Category */
$availableOrders = $category->getAvailableSortByOptions();
unset($availableOrders['position']);
$this->getListBlock()->setAvailableOrders(
$availableOrders
)->setDefaultDirection(
'desc'
)->setSortBy(
'relevance'
);
return $this;
}
/**
* Set available view mode
*
* @return $this
*/
public function setListModes()
{
$test = $this->getListBlock();
$test->setModes(['grid' => __('Grid'), 'list' => __('List')]);
return $this;
}
/**
* Retrieve Search result list HTML output
*
* @return string
*/
public function getProductListHtml()
{
return $this->getChildHtml('search_result_list');
}
/**
* Retrieve loaded category collection
*
* @return Collection
*/
protected function _getProductCollection()
{
if (null === $this->productCollection) {
$this->productCollection = $this->getListBlock()->getLoadedProductCollection();
}
return $this->productCollection;
}
/**
* Get search query text
*
* @return \Magento\Framework\Phrase
*/
public function getSearchQueryText()
{
return __("Search results for: '%1'", $this->catalogSearchData->getEscapedQueryText());
}
/**
* Retrieve search result count
*
* @return string
*/
public function getResultCount()
{
if (!$this->getData('result_count')) {
$size = $this->_getProductCollection()->getSize();
$this->_getQuery()->setNumResults($size);
$this->setResultCount($size);
}
return $this->getData('result_count');
}
/**
* Retrieve No Result or Minimum query length Text
*
* @return \Magento\Framework\Phrase|string
*/
public function getNoResultText()
{
if ($this->catalogSearchData->isMinQueryLength()) {
return __('Minimum Search query length is %1', $this->_getQuery()->getMinQueryLength());
}
return $this->_getData('no_result_text');
}
/**
* Retrieve Note messages
*
* @return array
*/
public function getNoteMessages()
{
return $this->catalogSearchData->getNoteMessages();
}
}