Result.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\CatalogSearch\Block;
  7. use Magento\Catalog\Block\Product\ListProduct;
  8. use Magento\Catalog\Model\Layer\Resolver as LayerResolver;
  9. use Magento\CatalogSearch\Helper\Data;
  10. use Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection;
  11. use Magento\Framework\View\Element\Template;
  12. use Magento\Framework\View\Element\Template\Context;
  13. use Magento\Search\Model\QueryFactory;
  14. /**
  15. * Product search result block
  16. *
  17. * @api
  18. * @since 100.0.2
  19. */
  20. class Result extends Template
  21. {
  22. /**
  23. * Catalog Product collection
  24. *
  25. * @var Collection
  26. */
  27. protected $productCollection;
  28. /**
  29. * Catalog search data
  30. *
  31. * @var Data
  32. */
  33. protected $catalogSearchData;
  34. /**
  35. * Catalog layer
  36. *
  37. * @var \Magento\Catalog\Model\Layer
  38. */
  39. protected $catalogLayer;
  40. /**
  41. * @var QueryFactory
  42. */
  43. private $queryFactory;
  44. /**
  45. * @param Context $context
  46. * @param LayerResolver $layerResolver
  47. * @param Data $catalogSearchData
  48. * @param QueryFactory $queryFactory
  49. * @param array $data
  50. */
  51. public function __construct(
  52. Context $context,
  53. LayerResolver $layerResolver,
  54. Data $catalogSearchData,
  55. QueryFactory $queryFactory,
  56. array $data = []
  57. ) {
  58. $this->catalogLayer = $layerResolver->get();
  59. $this->catalogSearchData = $catalogSearchData;
  60. $this->queryFactory = $queryFactory;
  61. parent::__construct($context, $data);
  62. }
  63. /**
  64. * Retrieve query model object
  65. *
  66. * @return \Magento\Search\Model\Query
  67. */
  68. protected function _getQuery()
  69. {
  70. return $this->queryFactory->get();
  71. }
  72. /**
  73. * Prepare layout
  74. *
  75. * @return $this
  76. */
  77. protected function _prepareLayout()
  78. {
  79. $title = $this->getSearchQueryText();
  80. $this->pageConfig->getTitle()->set($title);
  81. // add Home breadcrumb
  82. $breadcrumbs = $this->getLayout()->getBlock('breadcrumbs');
  83. if ($breadcrumbs) {
  84. $breadcrumbs->addCrumb(
  85. 'home',
  86. [
  87. 'label' => __('Home'),
  88. 'title' => __('Go to Home Page'),
  89. 'link' => $this->_storeManager->getStore()->getBaseUrl()
  90. ]
  91. )->addCrumb(
  92. 'search',
  93. ['label' => $title, 'title' => $title]
  94. );
  95. }
  96. return parent::_prepareLayout();
  97. }
  98. /**
  99. * Retrieve additional blocks html
  100. *
  101. * @return string
  102. */
  103. public function getAdditionalHtml()
  104. {
  105. return $this->getLayout()->getBlock('search_result_list')->getChildHtml('additional');
  106. }
  107. /**
  108. * Retrieve search list toolbar block
  109. *
  110. * @return ListProduct
  111. */
  112. public function getListBlock()
  113. {
  114. return $this->getChildBlock('search_result_list');
  115. }
  116. /**
  117. * Set search available list orders
  118. *
  119. * @return $this
  120. */
  121. public function setListOrders()
  122. {
  123. $category = $this->catalogLayer->getCurrentCategory();
  124. /* @var $category \Magento\Catalog\Model\Category */
  125. $availableOrders = $category->getAvailableSortByOptions();
  126. unset($availableOrders['position']);
  127. $availableOrders['relevance'] = __('Relevance');
  128. $this->getListBlock()->setAvailableOrders(
  129. $availableOrders
  130. )->setDefaultDirection(
  131. 'desc'
  132. )->setDefaultSortBy(
  133. 'relevance'
  134. );
  135. return $this;
  136. }
  137. /**
  138. * Set available view mode
  139. *
  140. * @return $this
  141. */
  142. public function setListModes()
  143. {
  144. $test = $this->getListBlock();
  145. $test->setModes(['grid' => __('Grid'), 'list' => __('List')]);
  146. return $this;
  147. }
  148. /**
  149. * Retrieve Search result list HTML output
  150. *
  151. * @return string
  152. */
  153. public function getProductListHtml()
  154. {
  155. return $this->getChildHtml('search_result_list');
  156. }
  157. /**
  158. * Retrieve loaded category collection
  159. *
  160. * @return Collection
  161. */
  162. protected function _getProductCollection()
  163. {
  164. if (null === $this->productCollection) {
  165. $this->productCollection = $this->getListBlock()->getLoadedProductCollection();
  166. }
  167. return $this->productCollection;
  168. }
  169. /**
  170. * Get search query text
  171. *
  172. * @return \Magento\Framework\Phrase
  173. */
  174. public function getSearchQueryText()
  175. {
  176. return __("Search results for: '%1'", $this->catalogSearchData->getEscapedQueryText());
  177. }
  178. /**
  179. * Retrieve search result count
  180. *
  181. * @return string
  182. */
  183. public function getResultCount()
  184. {
  185. if (!$this->getData('result_count')) {
  186. $size = $this->_getProductCollection()->getSize();
  187. $this->_getQuery()->saveNumResults($size);
  188. $this->setResultCount($size);
  189. }
  190. return $this->getData('result_count');
  191. }
  192. /**
  193. * Retrieve No Result or Minimum query length Text
  194. *
  195. * @return \Magento\Framework\Phrase|string
  196. */
  197. public function getNoResultText()
  198. {
  199. if ($this->catalogSearchData->isMinQueryLength()) {
  200. return __('Minimum Search query length is %1', $this->_getQuery()->getMinQueryLength());
  201. }
  202. return $this->_getData('no_result_text');
  203. }
  204. /**
  205. * Retrieve Note messages
  206. *
  207. * @return array
  208. */
  209. public function getNoteMessages()
  210. {
  211. return $this->catalogSearchData->getNoteMessages();
  212. }
  213. }