-
Notifications
You must be signed in to change notification settings - Fork 340
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Index attribute label for boolean attributes. Also adding a dedicated…
… filter model for booleans.
- Loading branch information
1 parent
13c42a7
commit 608e021
Showing
5 changed files
with
124 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
src/module-elasticsuite-catalog/Model/Layer/Filter/Boolean.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?php | ||
/** | ||
* DISCLAIMER | ||
* Do not edit or add to this file if you wish to upgrade Smile Elastic Suite to newer | ||
* versions in the future. | ||
* | ||
* @category Smile | ||
* @package Smile_ElasticSuiteCatalog | ||
* @author Romain Ruaud <[email protected]> | ||
* @copyright 2016 Smile | ||
* @license Open Software License ("OSL") v. 3.0 | ||
*/ | ||
namespace Smile\ElasticSuiteCatalog\Model\Layer\Filter; | ||
|
||
/** | ||
* Product boolean filter implementation. | ||
* | ||
* @category Smile | ||
* @package Smile_ElasticSuiteCatalog | ||
* @author Romain Ruaud <[email protected]> | ||
*/ | ||
class Boolean extends Attribute | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function apply(\Magento\Framework\App\RequestInterface $request) | ||
{ | ||
$attributeValue = $request->getParam($this->_requestVar); | ||
|
||
if (!empty($attributeValue)) { | ||
if (!is_array($attributeValue)) { | ||
$attributeValue = [$attributeValue]; | ||
} | ||
|
||
$this->currentFilterValue = $attributeValue; | ||
|
||
/** @var \Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection $productCollection */ | ||
$productCollection = $this->getLayer()->getProductCollection(); | ||
|
||
$productCollection->addFieldToFilter($this->getFilterField(), $attributeValue); | ||
$layerState = $this->getLayer()->getState(); | ||
|
||
$booleanFilterLabels = []; | ||
foreach ($this->currentFilterValue as $currentFilter) { | ||
$booleanFilterLabels[] = (string) $this->getAttributeModel()->getSource()->getOptionText((int) $currentFilter); | ||
} | ||
$filterLabel = implode(', ', $booleanFilterLabels); | ||
|
||
$filter = $this->_createItem($filterLabel, $this->currentFilterValue); | ||
|
||
$layerState->addFilter($filter); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Retrieve ES filter field. | ||
* | ||
* @return string | ||
*/ | ||
protected function getFilterField() | ||
{ | ||
$field = $this->getAttributeModel()->getAttributeCode(); | ||
|
||
return $field; | ||
} | ||
|
||
/** | ||
* @SuppressWarnings(PHPMD.CamelCaseMethodName) | ||
* @SuppressWarnings(PHPMD.ElseExpression) | ||
* | ||
* {@inheritDoc} | ||
*/ | ||
protected function _initItems() | ||
{ | ||
parent::_initItems(); | ||
|
||
foreach ($this->_items as $item) { | ||
$applyValue = $item->getLabel(); | ||
|
||
if ($item->getValue() == \Magento\Eav\Model\Entity\Attribute\Source\Boolean::VALUE_YES) { | ||
if (is_numeric($item->getLabel())) { | ||
$label = $this->getAttributeModel()->getSource()->getOptionText((int) $item->getLabel()); | ||
$item->setLabel((string) $label); | ||
} | ||
} | ||
|
||
if (($valuePos = array_search($applyValue, $this->currentFilterValue)) !== false) { | ||
$item->setIsSelected(true); | ||
$applyValue = $this->currentFilterValue; | ||
unset($applyValue[$valuePos]); | ||
} else { | ||
$applyValue = array_merge($this->currentFilterValue, [$applyValue]); | ||
} | ||
|
||
$item->setApplyFilterValue(array_values($applyValue)); | ||
} | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters