Skip to content

Commit

Permalink
fix: active state in side menu for nested child admins
Browse files Browse the repository at this point in the history
  • Loading branch information
fastnloud committed Feb 17, 2024
1 parent fb06e46 commit 07f9b8d
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions src/Menu/Matcher/Voter/AdminVoter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<object> $admin
*/
private function hasChildren(AdminInterface $admin): bool
{
return [] !== $admin->getChildren();
}

/**
* @param array<int, AdminInterface<object>> $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;
}
}

0 comments on commit 07f9b8d

Please sign in to comment.