Skip to content

Commit

Permalink
Merge pull request #136 from delyriand/fix/taxons-filter
Browse files Browse the repository at this point in the history
For taxon queries, add only child taxons of the current taxon in filters
  • Loading branch information
maximehuran committed Sep 20, 2022
2 parents eb12498 + 89c0b22 commit 7228e97
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/security.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ jobs:
- name: Install PHP dependencies
run: composer update --prefer-dist

- uses: symfonycorp/security-checker-action@v2
- uses: symfonycorp/security-checker-action@v3
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@
use MonsieurBiz\SyliusSearchPlugin\Model\Documentable\DocumentableInterface;
use MonsieurBiz\SyliusSearchPlugin\Search\Filter\Filter;
use MonsieurBiz\SyliusSearchPlugin\Search\Request\RequestConfiguration;
use MonsieurBiz\SyliusSearchPlugin\Search\Request\RequestInterface;
use MonsieurBiz\SyliusSearchPlugin\Search\Response\FilterBuilders\FilterBuilderInterface;
use Sylius\Component\Taxonomy\Model\TaxonInterface;

class TaxonsFilterBuilder
class TaxonsFilterBuilder implements FilterBuilderInterface
{
/**
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function build(
DocumentableInterface $documentable,
RequestConfiguration $requestConfiguration,
Expand All @@ -29,6 +35,13 @@ public function build(
return null;
}

$childTaxonCodes = null;
if (RequestInterface::TAXON_TYPE == $requestConfiguration->getType()) {
$childTaxonCodes = $requestConfiguration->getTaxon()->getChildren()->map(function (TaxonInterface $taxon): ?string {
return $taxon->getCode();
});
}

$taxonAggregation = $aggregationData['taxons']['taxons']['taxons'] ?? null;
if ($taxonAggregation && $taxonAggregation['doc_count'] > 0) {
$filter = new Filter($requestConfiguration, 'taxons', 'monsieurbiz_searchplugin.filters.taxon_filter', $taxonAggregation['doc_count']);
Expand All @@ -40,6 +53,10 @@ public function build(
continue;
}
$taxonCode = $taxonCodeBucket['key'];
// If we have a current taxon, add only the filter for the children of this taxon.
if (null !== $childTaxonCodes && !\in_array($taxonCode, $childTaxonCodes->toArray(), true)) {
continue;
}
$taxonName = null;
$taxonNameBuckets = $taxonCodeBucket['names']['buckets'] ?? [];
foreach ($taxonNameBuckets as $taxonNameBucket) {
Expand Down

0 comments on commit 7228e97

Please sign in to comment.