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 
8 use Magento\Catalog\Model\Layer\Resolver as LayerResolver;
15 
22 class Result extends Template
23 {
29  protected $_urlFactory;
30 
36  protected $_catalogLayer;
37 
44 
52  public function __construct(
53  Context $context,
54  Advanced $catalogSearchAdvanced,
55  LayerResolver $layerResolver,
56  UrlFactory $urlFactory,
57  array $data = []
58  ) {
59  $this->_catalogSearchAdvanced = $catalogSearchAdvanced;
60  $this->_catalogLayer = $layerResolver->get();
61  $this->_urlFactory = $urlFactory;
62  parent::__construct($context, $data);
63  }
64 
68  protected function _prepareLayout()
69  {
70  $this->pageConfig->getTitle()->set($this->getPageTitle());
71  $breadcrumbs = $this->getLayout()->getBlock('breadcrumbs');
72  if ($breadcrumbs) {
73  $breadcrumbs->addCrumb(
74  'home',
75  [
76  'label' => __('Home'),
77  'title' => __('Go to Home Page'),
78  'link' => $this->_storeManager->getStore()->getBaseUrl()
79  ]
80  )->addCrumb(
81  'search',
82  ['label' => __('Catalog Advanced Search'), 'link' => $this->getUrl('*/*/')]
83  )->addCrumb(
84  'search_result',
85  ['label' => __('Results')]
86  );
87  }
88  return parent::_prepareLayout();
89  }
90 
96  private function getPageTitle()
97  {
98  return __('Advanced Search Results');
99  }
100 
106  public function setListOrders()
107  {
108  /* @var $category \Magento\Catalog\Model\Category */
109  $category = $this->_catalogLayer->getCurrentCategory();
110 
111  $availableOrders = $category->getAvailableSortByOptions();
112  unset($availableOrders['position']);
113 
114  $this->getChildBlock('search_result_list')->setAvailableOrders($availableOrders);
115  }
116 
122  public function setListModes()
123  {
124  $this->getChildBlock('search_result_list')->setModes(['grid' => __('Grid'), 'list' => __('List')]);
125  }
126 
132  public function setListCollection()
133  {
134  $this->getChildBlock('search_result_list')->setCollection($this->_getProductCollection());
135  }
136 
142  protected function _getProductCollection()
143  {
144  return $this->getSearchModel()->getProductCollection();
145  }
146 
152  public function getSearchModel()
153  {
155  }
156 
162  public function getResultCount()
163  {
164  if (!$this->getData('result_count')) {
165  $size = $this->getSearchModel()->getProductCollection()->getSize();
166  $this->setResultCount($size);
167  }
168  return $this->getData('result_count');
169  }
170 
176  public function getProductListHtml()
177  {
178  return $this->getChildHtml('search_result_list');
179  }
180 
186  public function getFormUrl()
187  {
188  return $this->_urlFactory->create()->addQueryParams(
189  $this->getRequest()->getQueryValue()
190  )->getUrl(
191  '*/*/',
192  ['_escape' => true]
193  );
194  }
195 
201  public function getSearchCriterias()
202  {
203  $searchCriterias = $this->getSearchModel()->getSearchCriterias();
204  $middle = ceil(count($searchCriterias) / 2);
205  $left = array_slice($searchCriterias, 0, $middle);
206  $right = array_slice($searchCriterias, $middle);
207 
208  return ['left' => $left, 'right' => $right];
209  }
210 }
getData($key='', $index=null)
Definition: DataObject.php:119
__()
Definition: __.php:13
$middle
Definition: name.phtml:26
__construct(Context $context, Advanced $catalogSearchAdvanced, LayerResolver $layerResolver, UrlFactory $urlFactory, array $data=[])
Definition: Result.php:52
$searchCriterias
Definition: result.phtml:31