Skip to content
This repository has been archived by the owner on Oct 19, 2022. It is now read-only.

Commit

Permalink
Added active filter items to state and implemented getClearUrl method,
Browse files Browse the repository at this point in the history
…fixes #22
  • Loading branch information
Freek Gruntjes committed Dec 14, 2017
1 parent 84631d6 commit 02d991a
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 50 deletions.
49 changes: 49 additions & 0 deletions src/Block/LayeredNavigation/Navigation/State/Plugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* Tweakwise & Emico (https://www.tweakwise.com/ & https://www.emico.nl/) - All Rights Reserved
*
* @copyright Copyright (c) 2017-2017 Tweakwise.com B.V. (https://www.tweakwise.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/

namespace Emico\Tweakwise\Block\LayeredNavigation\Navigation\State;

use Closure;
use Emico\Tweakwise\Model\Catalog\Layer\Url;
use Emico\Tweakwise\Model\Config;
use Magento\LayeredNavigation\Block\Navigation\State;

class Plugin
{
/**
* @var Url
*/
private $url;

/**
* Plugin constructor.
*
* @param Config $config
* @param Url $url
*/
public function __construct(Config $config, Url $url)
{
$this->config = $config;
$this->url = $url;
}

/**
* @param State $subject
* @param Closure $proceed
* @return array
*/
public function aroundGetClearUrl(State $subject, Closure $proceed)
{
if (!$this->config->isLayeredEnabled()) {
return $proceed();
}

return $this->url->getClearUrl($subject->getActiveFilters());
}

}
15 changes: 15 additions & 0 deletions src/Model/Catalog/Layer/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ public function getItemsCount()
}

/**
* @return Item[]
* {@inheritdoc}
*/
public function getItems()
Expand Down Expand Up @@ -196,6 +197,20 @@ public function getLayer()
return $this->layer;
}

/**
* @return Item[]
*/
public function getActiveItems()
{
$result = [];
foreach ($this->getItems() as $item) {
if ($item->isActive()) {
$result[] = $item;
}
}
return $result;
}

/**
* {@inheritdoc}
*/
Expand Down
29 changes: 24 additions & 5 deletions src/Model/Catalog/Layer/Filter/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Emico\Tweakwise\Model\Catalog\Layer\Url;
use Emico\Tweakwise\Model\Client\Type\AttributeType;
use Emico\Tweakwise\Model\Client\Type\FacetType\SettingsType;
use Magento\Catalog\Model\Layer\Filter\Item as BaseItem;
use Magento\Catalog\Model\Layer\Filter\Item as MagentoItem;

/**
Expand Down Expand Up @@ -105,13 +104,25 @@ public function getValue()
public function getUrl()
{
$settings = $this->getFilter()->getFacet()->getFacetSettings();
if ($settings->getSelectionType() == SettingsType::SELECTION_TYPE_SLIDER) {
if ($settings->getSelectionType() === SettingsType::SELECTION_TYPE_SLIDER) {
return $this->url->getSliderUrl($this->getFilter());
} elseif ($this->isSelected()) {
}

if ($this->isSelected()) {
return $this->url->getRemoveFilter($this);
} else {
return $this->url->getSelectFilter($this);
}

return $this->url->getSelectFilter($this);
}

/**
* Get url for remove item from filter
*
* @return string
*/
public function getRemoveUrl()
{
return $this->url->getRemoveFilter($this);
}

/**
Expand Down Expand Up @@ -147,4 +158,12 @@ public function getChildren()
{
return $this->children;
}

/**
* @return bool
*/
public function isActive()
{
return $this->getAttribute()->getIsSelected();
}
}
4 changes: 4 additions & 0 deletions src/Model/Catalog/Layer/FilterList/Tweakwise.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ protected function initFilters(Layer $layer)
}

$this->filters[] = $filter;

foreach ($filter->getActiveItems() as $activeFilterItem) {
$layer->getState()->addFilter($activeFilterItem);
}
}

return $this;
Expand Down
13 changes: 8 additions & 5 deletions src/Model/Catalog/Layer/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,27 +43,30 @@ public function __construct(UrlInterface $implementation, HttpRequest $request)
}

/**
* {@inheritdoc}
* @param Item $item
* @return string
*/
public function getSelectFilter(Item $item)
{
return $this->implementation->getSelectFilter($this->request, $item);
}

/**
* {@inheritdoc}
* @param Item $item
* @return string
*/
public function getRemoveFilter(Item $item)
{
return $this->implementation->getRemoveFilter($this->request, $item);
}

/**
* {@inheritdoc}
* @param Item[] $activeFilterItems
* @return string
*/
public function getClearUrl(Filter $facet)
public function getClearUrl(array $activeFilterItems)
{
return $this->implementation->getClearUrl($this->request, $facet);
return $this->implementation->getClearUrl($this->request, $activeFilterItems);
}

/**
Expand Down
24 changes: 0 additions & 24 deletions src/Model/Catalog/Layer/Url/AbstractUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,21 +100,6 @@ public function getRemoveFilter(HttpRequest $request, Item $item)
}
}

/**
* {@inheritdoc}
*/
public function getClearUrl(HttpRequest $request, Filter $filter)
{
$facet = $filter->getFacet();
$settings = $facet->getFacetSettings();

if ($settings->getSource() == SettingsType::SOURCE_CATEGORY) {
return '#';
} else {
return $this->getAttributeCleanUrl($request, $filter);
}
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -309,15 +294,6 @@ protected abstract function getAttributeSelectUrl(HttpRequest $request, Item $it
*/
protected abstract function getAttributeRemoveUrl(HttpRequest $request, Item $item);

/**
* Get url when all attribute options are removed
*
* @param HttpRequest $request
* @param Filter $filter
* @return string
*/
protected abstract function getAttributeCleanUrl(HttpRequest $request, Filter $filter);

/**
* Fetch a list of category ID's to filter
* @param HttpRequest $request
Expand Down
34 changes: 20 additions & 14 deletions src/Model/Catalog/Layer/Url/QueryParameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,26 @@ class QueryParameters extends AbstractUrl
self::PARAM_SEARCH,
];

/**
* {@inheritdoc}
*/
public function getClearUrl(HttpRequest $request, array $activeFilterItems)
{
$query = [];
/** @var Item $item */
foreach ($activeFilterItems as $item) {
$filter = $item->getFilter();
$facet = $filter->getFacet();
$settings = $facet->getFacetSettings();

$urlKey = $settings->getUrlKey();

$query[$urlKey] = $filter->getCleanValue();
}

return $this->getCurrentQueryUrl($query);
}

/**
* @param array $query
* @return string
Expand Down Expand Up @@ -233,20 +253,6 @@ protected function getAttributeRemoveUrl(HttpRequest $request, Item $item)
return $this->getCurrentQueryUrl($query);
}

/**
* {@inheritdoc}
*/
protected function getAttributeCleanUrl(HttpRequest $request, Filter $filter)
{
$facet = $filter->getFacet();
$settings = $facet->getFacetSettings();

$urlKey = $settings->getUrlKey();

$query = [$urlKey => $filter->getResetValue()];
return $this->getCurrentQueryUrl($query);
}

/**
* {@inheritdoc}
*/
Expand Down
4 changes: 2 additions & 2 deletions src/Model/Catalog/Layer/Url/UrlInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ public function getSlider(HttpRequest $request, Filter $filter);
* Fetch clear all items from url
*
* @param HttpRequest $request
* @param Filter $filter
* @param Item[] $activeFilterItems
* @return string
*/
public function getClearUrl(HttpRequest $request, Filter $filter);
public function getClearUrl(HttpRequest $request, array $activeFilterItems);

/**
* Apply all attribute filters, category filters, sort order, page limit request parameters to navigation request
Expand Down
3 changes: 3 additions & 0 deletions src/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
<argument name="tweakwiseFilterList" xsi:type="object">Emico\Tweakwise\Model\Catalog\Layer\FilterList\Tweakwise\Proxy</argument>
</arguments>
</type>
<type name="Magento\LayeredNavigation\Block\Navigation\State">
<plugin name="emico-tweakwise" type="Emico\Tweakwise\Block\LayeredNavigation\Navigation\State\Plugin" sortOrder="0" />
</type>
<type name="Magento\Catalog\Block\Product\ProductList\Toolbar">
<plugin name="emico-tweakwise" type="Emico\Tweakwise\Block\Catalog\Product\ProductList\Toolbar\Plugin" sortOrder="0" />
</type>
Expand Down

0 comments on commit 02d991a

Please sign in to comment.