Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Attribute.php
Go to the documentation of this file.
1 <?php
7 
13 class Attribute extends \Magento\Catalog\Model\Layer\Filter\AbstractFilter
14 {
20  protected $_resource;
21 
27  protected $string;
28 
32  protected $tagFilter;
33 
44  public function __construct(
45  \Magento\Catalog\Model\Layer\Filter\ItemFactory $filterItemFactory,
47  \Magento\Catalog\Model\Layer $layer,
48  \Magento\Catalog\Model\Layer\Filter\Item\DataBuilder $itemDataBuilder,
49  \Magento\Catalog\Model\ResourceModel\Layer\Filter\AttributeFactory $filterAttributeFactory,
50  \Magento\Framework\Stdlib\StringUtils $string,
51  \Magento\Framework\Filter\StripTags $tagFilter,
52  array $data = []
53  ) {
54  $this->_resource = $filterAttributeFactory->create();
55  $this->string = $string;
56  $this->_requestVar = 'attribute';
57  $this->tagFilter = $tagFilter;
58  parent::__construct($filterItemFactory, $storeManager, $layer, $itemDataBuilder, $data);
59  }
60 
66  protected function _getResource()
67  {
68  return $this->_resource;
69  }
70 
77  public function apply(\Magento\Framework\App\RequestInterface $request)
78  {
79  $filter = $request->getParam($this->_requestVar);
80  if (is_array($filter)) {
81  return $this;
82  }
83  $text = $this->getOptionText($filter);
84  if ($filter && strlen($text)) {
85  $this->_getResource()->applyFilterToCollection($this, $filter);
86  $this->getLayer()->getState()->addFilter($this->_createItem($text, $filter));
87  $this->_items = [];
88  }
89  return $this;
90  }
91 
98  protected function _getItemsData()
99  {
100  $attribute = $this->getAttributeModel();
101  $this->_requestVar = $attribute->getAttributeCode();
102 
103  $options = $attribute->getFrontend()->getSelectOptions();
104  $optionsCount = $this->_getResource()->getCount($this);
105  foreach ($options as $option) {
106  if (is_array($option['value'])) {
107  continue;
108  }
109  if ($this->string->strlen($option['value'])) {
110  // Check filter type
112  if (!empty($optionsCount[$option['value']])) {
113  $this->itemDataBuilder->addItemData(
114  $this->tagFilter->filter($option['label']),
115  $option['value'],
116  $optionsCount[$option['value']]
117  );
118  }
119  } else {
120  $this->itemDataBuilder->addItemData(
121  $this->tagFilter->filter($option['label']),
122  $option['value'],
123  isset($optionsCount[$option['value']]) ? $optionsCount[$option['value']] : 0
124  );
125  }
126  }
127  }
128 
129  return $this->itemDataBuilder->build();
130  }
131 }
$storeManager
endifif( $block->getLastPageNum()>1)( 'Page') ?></strong >< ul class $text
Definition: pager.phtml:43
__construct(\Magento\Catalog\Model\Layer\Filter\ItemFactory $filterItemFactory, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Catalog\Model\Layer $layer, \Magento\Catalog\Model\Layer\Filter\Item\DataBuilder $itemDataBuilder, \Magento\Catalog\Model\ResourceModel\Layer\Filter\AttributeFactory $filterAttributeFactory, \Magento\Framework\Stdlib\StringUtils $string, \Magento\Framework\Filter\StripTags $tagFilter, array $data=[])
Definition: Attribute.php:44
apply(\Magento\Framework\App\RequestInterface $request)
Definition: Attribute.php:77