Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Layer.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Catalog\Model;
7 
10 use Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory as AttributeCollectionFactory;
11 
21 {
27  protected $_productCollections = [];
28 
34  protected $_stateKey = null;
35 
41  protected $registry = null;
42 
48  protected $_storeManager;
49 
55  protected $_catalogProduct;
56 
63 
70 
75 
79  protected $stateKeyGenerator;
80 
84  protected $collectionFilter;
85 
90 
101  public function __construct(
102  \Magento\Catalog\Model\Layer\ContextInterface $context,
103  \Magento\Catalog\Model\Layer\StateFactory $layerStateFactory,
104  AttributeCollectionFactory $attributeCollectionFactory,
105  \Magento\Catalog\Model\ResourceModel\Product $catalogProduct,
107  \Magento\Framework\Registry $registry,
109  array $data = []
110  ) {
111  $this->_layerStateFactory = $layerStateFactory;
112  $this->_attributeCollectionFactory = $attributeCollectionFactory;
113  $this->_catalogProduct = $catalogProduct;
114  $this->_storeManager = $storeManager;
115  $this->registry = $registry;
116  $this->categoryRepository = $categoryRepository;
117  $this->collectionProvider = $context->getCollectionProvider();
118  $this->stateKeyGenerator = $context->getStateKey();
119  $this->collectionFilter = $context->getCollectionFilter();
120  parent::__construct($data);
121  }
122 
128  public function getStateKey()
129  {
130  if (!$this->_stateKey) {
131  $this->_stateKey = $this->stateKeyGenerator->toString($this->getCurrentCategory());
132  }
133  return $this->_stateKey;
134  }
135 
141  public function getProductCollection()
142  {
143  if (isset($this->_productCollections[$this->getCurrentCategory()->getId()])) {
144  $collection = $this->_productCollections[$this->getCurrentCategory()->getId()];
145  } else {
146  $collection = $this->collectionProvider->getCollection($this->getCurrentCategory());
148  $this->_productCollections[$this->getCurrentCategory()->getId()] = $collection;
149  }
150 
151  return $collection;
152  }
153 
161  {
162  $this->collectionFilter->filter($collection, $this->getCurrentCategory());
163 
164  return $this;
165  }
166 
175  public function apply()
176  {
177  $stateSuffix = '';
178  foreach ($this->getState()->getFilters() as $filterItem) {
179  $stateSuffix .= '_' . $filterItem->getFilter()->getRequestVar() . '_' . $filterItem->getValueString();
180  }
181  if (!empty($stateSuffix)) {
182  $this->_stateKey = $this->getStateKey() . $stateSuffix;
183  }
184 
185  return $this;
186  }
187 
194  public function getCurrentCategory()
195  {
196  $category = $this->getData('current_category');
197  if ($category === null) {
198  $category = $this->registry->registry('current_category');
199  if ($category) {
200  $this->setData('current_category', $category);
201  } else {
202  $category = $this->categoryRepository->get($this->getCurrentStore()->getRootCategoryId());
203  $this->setData('current_category', $category);
204  }
205  }
206 
207  return $category;
208  }
209 
217  public function setCurrentCategory($category)
218  {
219  if (is_numeric($category)) {
220  try {
221  $category = $this->categoryRepository->get($category);
222  } catch (NoSuchEntityException $e) {
223  throw new \Magento\Framework\Exception\LocalizedException(__('Please correct the category.'), $e);
224  }
225  } elseif ($category instanceof \Magento\Catalog\Model\Category) {
226  if (!$category->getId()) {
227  throw new \Magento\Framework\Exception\LocalizedException(__('Please correct the category.'));
228  }
229  } else {
230  throw new \Magento\Framework\Exception\LocalizedException(
231  __('Must be category model instance or its id.')
232  );
233  }
234 
235  if ($category->getId() != $this->getCurrentCategory()->getId()) {
236  $this->setData('current_category', $category);
237  }
238 
239  return $this;
240  }
241 
247  public function getCurrentStore()
248  {
249  return $this->_storeManager->getStore();
250  }
251 
257  public function getState()
258  {
259  $state = $this->getData('state');
260  if ($state === null) {
261  \Magento\Framework\Profiler::start(__METHOD__);
262  $state = $this->_layerStateFactory->create();
263  $this->setData('state', $state);
264  \Magento\Framework\Profiler::stop(__METHOD__);
265  }
266 
267  return $state;
268  }
269 }
prepareProductCollection($collection)
Definition: Layer.php:160
getData($key='', $index=null)
Definition: DataObject.php:119
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$storeManager
__()
Definition: __.php:13
__construct(\Magento\Catalog\Model\Layer\ContextInterface $context, \Magento\Catalog\Model\Layer\StateFactory $layerStateFactory, AttributeCollectionFactory $attributeCollectionFactory, \Magento\Catalog\Model\ResourceModel\Product $catalogProduct, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\Registry $registry, CategoryRepositoryInterface $categoryRepository, array $data=[])
Definition: Layer.php:101
setCurrentCategory($category)
Definition: Layer.php:217
setData($key, $value=null)
Definition: DataObject.php:72