Skip to content

Commit

Permalink
fix regexp when category being filtered is root category
Browse files Browse the repository at this point in the history
  • Loading branch information
mhhansen committed Jul 12, 2018
1 parent 6b7a9b3 commit 7b1c4dd
Showing 1 changed file with 10 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,17 @@ public function getTree(ResolveInfo $resolveInfo, int $rootCategoryId) : array
$this->joinAttributesRecursively($collection, $categoryQuery);
$depth = $this->depthCalculator->calculate($categoryQuery);
$level = $this->levelCalculator->calculate($rootCategoryId);

// If root category is being filter, we've to remove first slash
if($rootCategoryId == 1) {
$regExpPathFilter = sprintf('.*%s/[/0-9]*$', $rootCategoryId);
} else {
$regExpPathFilter = sprintf('.*/%s/[/0-9]*$', $rootCategoryId);
}

//Search for desired part of category tree
$collection->addPathFilter(sprintf('.*/%s/[/0-9]*$', $rootCategoryId));
$collection->addPathFilter($regExpPathFilter);

$collection->addFieldToFilter('level', ['gt' => $level]);
$collection->addFieldToFilter('level', ['lteq' => $level + $depth - self::DEPTH_OFFSET]);
$collection->setOrder('level');
Expand Down

0 comments on commit 7b1c4dd

Please sign in to comment.