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 16, 2024
1 parent fb06e46 commit 91ca394
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 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,41 @@ 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;
}

private function hasChildren(AdminInterface $admin): bool

Check failure on line 66 in src/Menu/Matcher/Voter/AdminVoter.php

View workflow job for this annotation

GitHub Actions / PHPStan

Method Sonata\AdminBundle\Menu\Matcher\Voter\AdminVoter::hasChildren() has parameter $admin with generic interface Sonata\AdminBundle\Admin\AdminInterface but does not specify its types: T
{
return [] !== $admin->getChildren();
}

private function matchChildren(array $children, mixed $requestCode): ?bool

Check failure on line 71 in src/Menu/Matcher/Voter/AdminVoter.php

View workflow job for this annotation

GitHub Actions / PHPStan

Method Sonata\AdminBundle\Menu\Matcher\Voter\AdminVoter::matchChildren() has parameter $children with no value type specified in iterable type array.
{
foreach ($children as $child) {
if ($child->getBaseCodeRoute() === $requestCode) {
return true;
}

if ($this->hasChildren($child)) {
return $this->matchChildren($child->getChildren(), $requestCode);
}
}

return null;
}
}

0 comments on commit 91ca394

Please sign in to comment.