From 518c8c4efb3e660deb9555a2692ecab2e93ebe34 Mon Sep 17 00:00:00 2001 From: Lorenzo Ruozzi Date: Wed, 24 Apr 2024 17:30:43 +0200 Subject: [PATCH] Allow to unset filter value --- src/Twig/FilterRuntime.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Twig/FilterRuntime.php b/src/Twig/FilterRuntime.php index eda8cd0..c78a42f 100644 --- a/src/Twig/FilterRuntime.php +++ b/src/Twig/FilterRuntime.php @@ -31,6 +31,9 @@ public function isFilterActive(FilterValueInterface $filterValue, FilterInterfac } /** + * If the filter value is already applied, it will be removed from the active filters, so that it can be toggled. + * Otherwise, it will be added to the active filters. + * * @param array> $activeFilters */ public function mergeFilterValueWithCurrentActiveFilters( @@ -42,9 +45,11 @@ public function mergeFilterValueWithCurrentActiveFilters( if (!array_key_exists($filterType, $activeFilters)) { $activeFilters[$filterType] = []; } - foreach ($activeFilters[$filterType] as $appliedFilterValue) { + foreach ($activeFilters[$filterType] as $key => $appliedFilterValue) { if ($appliedFilterValue['code'] === $filter->getKeyCode() && $appliedFilterValue['value'] === $filterValue->getKey()) { + unset($activeFilters[$filterType][$key]); + return $activeFilters; } }