Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Attributes
Layer Class Reference
Inheritance diagram for Layer:
DataObject Category Search

Public Member Functions

 __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=[])
 
 getStateKey ()
 
 getProductCollection ()
 
 prepareProductCollection ($collection)
 
 apply ()
 
 getCurrentCategory ()
 
 setCurrentCategory ($category)
 
 getCurrentStore ()
 
 getState ()
 
- Public Member Functions inherited from DataObject
 __construct (array $data=[])
 
 addData (array $arr)
 
 setData ($key, $value=null)
 
 unsetData ($key=null)
 
 getData ($key='', $index=null)
 
 getDataByPath ($path)
 
 getDataByKey ($key)
 
 setDataUsingMethod ($key, $args=[])
 
 getDataUsingMethod ($key, $args=null)
 
 hasData ($key='')
 
 toArray (array $keys=[])
 
 convertToArray (array $keys=[])
 
 toXml (array $keys=[], $rootName='item', $addOpenTag=false, $addCdata=true)
 
 convertToXml (array $arrAttributes=[], $rootName='item', $addOpenTag=false, $addCdata=true)
 
 toJson (array $keys=[])
 
 convertToJson (array $keys=[])
 
 toString ($format='')
 
 __call ($method, $args)
 
 isEmpty ()
 
 serialize ($keys=[], $valueSeparator='=', $fieldSeparator=' ', $quote='"')
 
 debug ($data=null, &$objects=[])
 
 offsetSet ($offset, $value)
 
 offsetExists ($offset)
 
 offsetUnset ($offset)
 
 offsetGet ($offset)
 

Protected Attributes

 $_productCollections = []
 
 $_stateKey = null
 
 $registry = null
 
 $_storeManager
 
 $_catalogProduct
 
 $_attributeCollectionFactory
 
 $_layerStateFactory
 
 $collectionProvider
 
 $stateKeyGenerator
 
 $collectionFilter
 
 $categoryRepository
 
- Protected Attributes inherited from DataObject
 $_data = []
 

Additional Inherited Members

- Protected Member Functions inherited from DataObject
 _getData ($key)
 
 _underscore ($name)
 
- Static Protected Attributes inherited from DataObject
static $_underscoreCache = []
 

Detailed Description

Catalog view layer model

@api @SuppressWarnings(PHPMD.LongVariable) @SuppressWarnings(PHPMD.CouplingBetweenObjects)

Since
100.0.2

Definition at line 20 of file Layer.php.

Constructor & Destructor Documentation

◆ __construct()

__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 = [] 
)
Parameters
Layer\ContextInterface$context
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 at line 101 of file Layer.php.

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  }
$storeManager

Member Function Documentation

◆ apply()

apply ( )

Apply layer Method is colling after apply all filters, can be used for prepare some index data before getting information about existing indexes

Returns
\Magento\Catalog\Model\Layer

Definition at line 175 of file Layer.php.

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  }

◆ getCurrentCategory()

getCurrentCategory ( )

Retrieve current category model If no category found in registry, the root will be taken

Returns
\Magento\Catalog\Model\Category

Definition at line 194 of file Layer.php.

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  }
getData($key='', $index=null)
Definition: DataObject.php:119
setData($key, $value=null)
Definition: DataObject.php:72

◆ getCurrentStore()

getCurrentStore ( )

Retrieve current store model

Returns
\Magento\Store\Model\Store

Definition at line 247 of file Layer.php.

248  {
249  return $this->_storeManager->getStore();
250  }

◆ getProductCollection()

getProductCollection ( )

Retrieve current layer product collection

Returns
\Magento\Catalog\Model\ResourceModel\Product\Collection

Definition at line 141 of file Layer.php.

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  }
prepareProductCollection($collection)
Definition: Layer.php:160

◆ getState()

getState ( )

Retrieve layer state object

Returns
\Magento\Catalog\Model\Layer\State

Definition at line 257 of file Layer.php.

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  }
getData($key='', $index=null)
Definition: DataObject.php:119
setData($key, $value=null)
Definition: DataObject.php:72

◆ getStateKey()

getStateKey ( )

Get layer state key

Returns
string

Definition at line 128 of file Layer.php.

129  {
130  if (!$this->_stateKey) {
131  $this->_stateKey = $this->stateKeyGenerator->toString($this->getCurrentCategory());
132  }
133  return $this->_stateKey;
134  }

◆ prepareProductCollection()

prepareProductCollection (   $collection)

Initialize product collection

Parameters
\Magento\Catalog\Model\ResourceModel\Product\Collection$collection
Returns
\Magento\Catalog\Model\Layer

Definition at line 160 of file Layer.php.

161  {
162  $this->collectionFilter->filter($collection, $this->getCurrentCategory());
163 
164  return $this;
165  }

◆ setCurrentCategory()

setCurrentCategory (   $category)

Change current category object

Parameters
mixed$category
Returns
\Magento\Catalog\Model\Layer
Exceptions

Definition at line 217 of file Layer.php.

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  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
__()
Definition: __.php:13
setData($key, $value=null)
Definition: DataObject.php:72

Field Documentation

◆ $_attributeCollectionFactory

$_attributeCollectionFactory
protected

Definition at line 62 of file Layer.php.

◆ $_catalogProduct

$_catalogProduct
protected

Definition at line 55 of file Layer.php.

◆ $_layerStateFactory

$_layerStateFactory
protected

Definition at line 69 of file Layer.php.

◆ $_productCollections

$_productCollections = []
protected

Definition at line 27 of file Layer.php.

◆ $_stateKey

$_stateKey = null
protected

Definition at line 34 of file Layer.php.

◆ $_storeManager

$_storeManager
protected

Definition at line 48 of file Layer.php.

◆ $categoryRepository

$categoryRepository
protected

Definition at line 89 of file Layer.php.

◆ $collectionFilter

$collectionFilter
protected

Definition at line 84 of file Layer.php.

◆ $collectionProvider

$collectionProvider
protected

Definition at line 74 of file Layer.php.

◆ $registry

$registry = null
protected

Definition at line 41 of file Layer.php.

◆ $stateKeyGenerator

$stateKeyGenerator
protected

Definition at line 79 of file Layer.php.


The documentation for this class was generated from the following file: