From e0d77dc17e0e63ad5c42aebe2a1e246c8af317b8 Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Mon, 12 Jul 2021 14:21:20 +0200 Subject: [PATCH] add criteria when fetching node children --- core/Entity/Site/Node.php | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/core/Entity/Site/Node.php b/core/Entity/Site/Node.php index 208e201..10bd792 100644 --- a/core/Entity/Site/Node.php +++ b/core/Entity/Site/Node.php @@ -225,12 +225,30 @@ class Node implements EntityInterface /** * @return Collection|Node[] */ - public function getChildren(): Collection + public function getChildren(array $criteria = []): Collection { if (null === $this->children) { $this->children = new ArrayCollection(); } + if (!empty($criteria)) { + $children = new ArrayCollection(); + + foreach ($this->children as $child) { + $add = true; + + if (isset($criteria['visible']) && $child->getIsVisible() !== $criteria['visible']) { + $add = false; + } + + if ($add) { + $children->add($child); + } + } + + return $children; + } + return $this->children; } @@ -256,7 +274,7 @@ class Node implements EntityInterface return $this; } - public function getAllChildren(): ArrayCollection + public function getAllChildren(array $criteria = []): ArrayCollection { $children = []; @@ -274,6 +292,14 @@ class Node implements EntityInterface return $a->getTreeLeft() < $b->getTreeLeft() ? -1 : 1; }); + if (!empty($criteria)) { + foreach ($children as $key => $child) { + if (isset($criteria['visible']) && $child->getIsVisible() !== $criteria['visible']) { + unset($children[$key]); + } + } + } + return new ArrayCollection($children); }