Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Result.php
Go to the documentation of this file.
1 <?php
7 
9 use Magento\Catalog\Model\Layer\Resolver as LayerResolver;
15 
22 class Result extends Template
23 {
29  protected $productCollection;
30 
36  protected $catalogSearchData;
37 
43  protected $catalogLayer;
44 
48  private $queryFactory;
49 
57  public function __construct(
58  Context $context,
59  LayerResolver $layerResolver,
61  QueryFactory $queryFactory,
62  array $data = []
63  ) {
64  $this->catalogLayer = $layerResolver->get();
65  $this->catalogSearchData = $catalogSearchData;
66  $this->queryFactory = $queryFactory;
67  parent::__construct($context, $data);
68  }
69 
75  protected function _getQuery()
76  {
77  return $this->queryFactory->get();
78  }
79 
85  protected function _prepareLayout()
86  {
87  $title = $this->getSearchQueryText();
88  $this->pageConfig->getTitle()->set($title);
89  // add Home breadcrumb
90  $breadcrumbs = $this->getLayout()->getBlock('breadcrumbs');
91  if ($breadcrumbs) {
92  $breadcrumbs->addCrumb(
93  'home',
94  [
95  'label' => __('Home'),
96  'title' => __('Go to Home Page'),
97  'link' => $this->_storeManager->getStore()->getBaseUrl()
98  ]
99  )->addCrumb(
100  'search',
101  ['label' => $title, 'title' => $title]
102  );
103  }
104 
105  return parent::_prepareLayout();
106  }
107 
113  public function getAdditionalHtml()
114  {
115  return $this->getLayout()->getBlock('search_result_list')->getChildHtml('additional');
116  }
117 
123  public function getListBlock()
124  {
125  return $this->getChildBlock('search_result_list');
126  }
127 
133  public function setListOrders()
134  {
135  $category = $this->catalogLayer->getCurrentCategory();
136  /* @var $category \Magento\Catalog\Model\Category */
137  $availableOrders = $category->getAvailableSortByOptions();
138  unset($availableOrders['position']);
139  $availableOrders['relevance'] = __('Relevance');
140 
141  $this->getListBlock()->setAvailableOrders(
142  $availableOrders
143  )->setDefaultDirection(
144  'desc'
145  )->setDefaultSortBy(
146  'relevance'
147  );
148 
149  return $this;
150  }
151 
157  public function setListModes()
158  {
159  $test = $this->getListBlock();
160  $test->setModes(['grid' => __('Grid'), 'list' => __('List')]);
161  return $this;
162  }
163 
169  public function getProductListHtml()
170  {
171  return $this->getChildHtml('search_result_list');
172  }
173 
179  protected function _getProductCollection()
180  {
181  if (null === $this->productCollection) {
182  $this->productCollection = $this->getListBlock()->getLoadedProductCollection();
183  }
184 
186  }
187 
193  public function getSearchQueryText()
194  {
195  return __("Search results for: '%1'", $this->catalogSearchData->getEscapedQueryText());
196  }
197 
203  public function getResultCount()
204  {
205  if (!$this->getData('result_count')) {
206  $size = $this->_getProductCollection()->getSize();
207  $this->_getQuery()->saveNumResults($size);
208  $this->setResultCount($size);
209  }
210  return $this->getData('result_count');
211  }
212 
218  public function getNoResultText()
219  {
220  if ($this->catalogSearchData->isMinQueryLength()) {
221  return __('Minimum Search query length is %1', $this->_getQuery()->getMinQueryLength());
222  }
223  return $this->_getData('no_result_text');
224  }
225 
231  public function getNoteMessages()
232  {
233  return $this->catalogSearchData->getNoteMessages();
234  }
235 }
$title
Definition: default.phtml:14
getData($key='', $index=null)
Definition: DataObject.php:119
__()
Definition: __.php:13
__construct(Context $context, LayerResolver $layerResolver, Data $catalogSearchData, QueryFactory $queryFactory, array $data=[])
Definition: Result.php:57