diff --git a/src/Menu/Matcher/Voter/AdminVoter.php b/src/Menu/Matcher/Voter/AdminVoter.php index 7876475abf..acdef58f8a 100644 --- a/src/Menu/Matcher/Voter/AdminVoter.php +++ b/src/Menu/Matcher/Voter/AdminVoter.php @@ -33,7 +33,6 @@ public function __construct( public function matchItem(ItemInterface $item): ?bool { $admin = $item->getExtra('admin'); - $request = $this->requestStack->getMainRequest(); if ($admin instanceof AdminInterface @@ -46,18 +45,47 @@ public function matchItem(ItemInterface $item): ?bool return true; } - foreach ($admin->getChildren() as $child) { - if ($child->getBaseCodeRoute() === $requestCode) { - return true; + if ($this->hasChildren($admin)) { + $isMatch = $this->matchChildren($admin->getChildren(), $requestCode); + + if (null !== $isMatch) { + return $isMatch; } } } $route = $item->getExtra('route'); + if (null !== $route && null !== $request && $route === $request->get('_route')) { return true; } return null; } + + /** + * @param AdminInterface $admin + */ + private function hasChildren(AdminInterface $admin): bool + { + return [] !== $admin->getChildren(); + } + + /** + * @param array> $children + */ + private function matchChildren(array $children, mixed $requestCode): ?bool + { + foreach ($children as $child) { + if ($child->getBaseCodeRoute() === $requestCode) { + return true; + } + + if ($this->hasChildren($child) && true === $this->matchChildren($child->getChildren(), $requestCode)) { + return true; + } + } + + return null; + } }