123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- <?php
- /**
- * Copyright © Magento, Inc. 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\ResourceModel\Fulltext\Collection;
- use Magento\Framework\View\Element\Template;
- use Magento\Framework\View\Element\Template\Context;
- use Magento\Search\Model\QueryFactory;
- /**
- * Product search result block
- *
- * @api
- * @since 100.0.2
- */
- 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']);
- $availableOrders['relevance'] = __('Relevance');
- $this->getListBlock()->setAvailableOrders(
- $availableOrders
- )->setDefaultDirection(
- 'desc'
- )->setDefaultSortBy(
- '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()->saveNumResults($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();
- }
- }
|