Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
FilterList.php
Go to the documentation of this file.
1 <?php
8 
9 class FilterList
10 {
11  const CATEGORY_FILTER = 'category';
12  const ATTRIBUTE_FILTER = 'attribute';
13  const PRICE_FILTER = 'price';
14  const DECIMAL_FILTER = 'decimal';
15 
21  protected $objectManager;
22 
27 
31  protected $filterTypes = [
32  self::CATEGORY_FILTER => \Magento\Catalog\Model\Layer\Filter\Category::class,
33  self::ATTRIBUTE_FILTER => \Magento\Catalog\Model\Layer\Filter\Attribute::class,
34  self::PRICE_FILTER => \Magento\Catalog\Model\Layer\Filter\Price::class,
35  self::DECIMAL_FILTER => \Magento\Catalog\Model\Layer\Filter\Decimal::class,
36  ];
37 
41  protected $filters = [];
42 
48  public function __construct(
51  array $filters = []
52  ) {
53  $this->objectManager = $objectManager;
54  $this->filterableAttributes = $filterableAttributes;
55 
57  $this->filterTypes = array_merge($this->filterTypes, $filters);
58  }
59 
66  public function getFilters(\Magento\Catalog\Model\Layer $layer)
67  {
68  if (!count($this->filters)) {
69  $this->filters = [
70  $this->objectManager->create($this->filterTypes[self::CATEGORY_FILTER], ['layer' => $layer]),
71  ];
72  foreach ($this->filterableAttributes->getList() as $attribute) {
73  $this->filters[] = $this->createAttributeFilter($attribute, $layer);
74  }
75  }
76  return $this->filters;
77  }
78 
86  protected function createAttributeFilter(
87  \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attribute,
88  \Magento\Catalog\Model\Layer $layer
89  ) {
90  $filterClassName = $this->getAttributeFilterClass($attribute);
91 
92  $filter = $this->objectManager->create(
93  $filterClassName,
94  ['data' => ['attribute_model' => $attribute], 'layer' => $layer]
95  );
96  return $filter;
97  }
98 
105  protected function getAttributeFilterClass(\Magento\Catalog\Model\ResourceModel\Eav\Attribute $attribute)
106  {
107  $filterClassName = $this->filterTypes[self::ATTRIBUTE_FILTER];
108 
109  if ($attribute->getAttributeCode() == 'price') {
110  $filterClassName = $this->filterTypes[self::PRICE_FILTER];
111  } elseif ($attribute->getBackendType() == 'decimal') {
112  $filterClassName = $this->filterTypes[self::DECIMAL_FILTER];
113  }
114 
115  return $filterClassName;
116  }
117 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
createAttributeFilter(\Magento\Catalog\Model\ResourceModel\Eav\Attribute $attribute, \Magento\Catalog\Model\Layer $layer)
Definition: FilterList.php:86
getFilters(\Magento\Catalog\Model\Layer $layer)
Definition: FilterList.php:66
__construct(\Magento\Framework\ObjectManagerInterface $objectManager, FilterableAttributeListInterface $filterableAttributes, array $filters=[])
Definition: FilterList.php:48
getAttributeFilterClass(\Magento\Catalog\Model\ResourceModel\Eav\Attribute $attribute)
Definition: FilterList.php:105