Skip to content

Commit

Permalink
Index attribute label for boolean attributes. Also adding a dedicated…
Browse files Browse the repository at this point in the history
… filter model for booleans.
  • Loading branch information
romainruaud committed Apr 7, 2016
1 parent 13c42a7 commit 608e021
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 7 deletions.
9 changes: 6 additions & 3 deletions src/module-elasticsuite-catalog/Helper/ProductAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,12 @@ public function getFieldType(ProductAttributeInterface $attribute)
{
$type = FieldInterface::FIELD_TYPE_STRING;

if ($attribute->getBackendType() == 'int' || $attribute->getFrontendClass() == 'validate-digits') {
if ($attribute->getSourceModel() == 'Magento\Eav\Model\Entity\Attribute\Source\Boolean') {
$type = FieldInterface::FIELD_TYPE_BOOLEAN;
} elseif ($attribute->getBackendType() == 'int' || $attribute->getFrontendClass() == 'validate-digits') {
$type = FieldInterface::FIELD_TYPE_INTEGER;
} elseif ($attribute->getBackendType() == 'decimal' || $attribute->getFrontendClass() == 'validate-number') {
$type = FieldInterface::FIELD_TYPE_DOUBLE;
} elseif ($attribute->getSourceModel() == 'eav/entity_attribute_source_boolean') {
$type = FieldInterface::FIELD_TYPE_BOOLEAN;
} elseif ($attribute->getBackendType() == 'datetime') {
$type = FieldInterface::FIELD_TYPE_DATE;
} elseif ($attribute->usesSource() && $attribute->getSourceModel() === null) {
Expand Down Expand Up @@ -221,6 +221,9 @@ public function getIndexOptionText(ProductAttributeInterface $attribute, $storeI

if (!isset($this->attributeOptionTextCache[$storeId][$attributeId][$optionId])) {
$optionValue = $attribute->getSource()->getIndexOptionText($optionId);
if ($this->getFieldType($attribute) == FieldInterface::FIELD_TYPE_BOOLEAN) {
$optionValue = $attribute->getStoreLabel($storeId);
}
$this->attributeOptionTextCache[$storeId][$attributeId][$optionId] = $optionValue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
namespace Smile\ElasticSuiteCatalog\Model\Layer\Filter;

use Smile\ElasticSuiteCore\Search\Request\BucketInterface;
use Magento\Catalog\Model\Layer\Filter\Item\DataBuilder;

/**
* Product attribute filter implementation.
Expand All @@ -29,8 +28,7 @@ class Attribute extends \Magento\CatalogSearch\Model\Layer\Filter\Attribute
/**
* @var array
*/
private $currentFilterValue = [];

protected $currentFilterValue = [];

/**
* @var \Magento\Framework\Filter\StripTags
Expand Down Expand Up @@ -171,7 +169,7 @@ protected function _initItems()
*
* @return string
*/
private function getFilterField()
protected function getFilterField()
{
$field = $this->getAttributeModel()->getAttributeCode();

Expand Down
103 changes: 103 additions & 0 deletions src/module-elasticsuite-catalog/Model/Layer/Filter/Boolean.php
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;
}
}
11 changes: 11 additions & 0 deletions src/module-elasticsuite-catalog/Model/Layer/FilterList.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
*/
class FilterList extends \Magento\Catalog\Model\Layer\FilterList
{
/**
* Boolean filter name
*/
const BOOLEAN_FILTER = 'boolean';

/**
* {@inheritDoc}
*/
Expand All @@ -33,6 +38,12 @@ protected function getAttributeFilterClass(\Magento\Catalog\Model\ResourceModel\
$filterClassName = $this->filterTypes[self::DECIMAL_FILTER];
}

if (($attribute->getFrontendInput() == 'boolean')
&& ($attribute->getSourceModel() == 'Magento\Eav\Model\Entity\Attribute\Source\Boolean')
&& isset($this->filterTypes[self::BOOLEAN_FILTER])) {
$filterClassName = $this->filterTypes[self::BOOLEAN_FILTER];
}

return $filterClassName;
}
}
2 changes: 2 additions & 0 deletions src/module-elasticsuite-catalog/etc/frontend/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<item name="price" xsi:type="string">Smile\ElasticSuiteCatalog\Model\Layer\Filter\Price</item>
<item name="decimal" xsi:type="string">Smile\ElasticSuiteCatalog\Model\Layer\Filter\Decimal</item>
<item name="category" xsi:type="string">categoryFilterUsingRewrite</item>
<item name="boolean" xsi:type="string">Smile\ElasticSuiteCatalog\Model\Layer\Filter\Boolean</item>
</argument>
</arguments>
</virtualType>
Expand All @@ -35,6 +36,7 @@
<item name="price" xsi:type="string">Smile\ElasticSuiteCatalog\Model\Layer\Filter\Price</item>
<item name="decimal" xsi:type="string">Smile\ElasticSuiteCatalog\Model\Layer\Filter\Decimal</item>
<item name="category" xsi:type="string">Smile\ElasticSuiteCatalog\Model\Layer\Filter\Category</item>
<item name="boolean" xsi:type="string">Smile\ElasticSuiteCatalog\Model\Layer\Filter\Boolean</item>
</argument>
</arguments>
</virtualType>
Expand Down

0 comments on commit 608e021

Please sign in to comment.