Skip to content

Commit

Permalink
bug #6734 Fix Dashboard menu item highlighting (javiereguiluz)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the 4.x branch.

Discussion
----------

Fix Dashboard menu item highlighting

Fixes #6701.

Commits
-------

c4a848c Fix Dashboard menu item highlighting
  • Loading branch information
javiereguiluz committed Jan 17, 2025
2 parents 533e89a + c4a848c commit 3aea6c9
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
1 change: 1 addition & 0 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@

->set(MenuItemMatcher::class)
->arg(0, service(AdminUrlGenerator::class))
->arg(1, service(AdminRouteGenerator::class))

->alias(MenuItemMatcherInterface::class, MenuItemMatcher::class)

Expand Down
5 changes: 3 additions & 2 deletions src/Menu/MenuItemMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use EasyCorp\Bundle\EasyAdminBundle\Config\Option\EA;
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Menu\MenuItemMatcherInterface;
use EasyCorp\Bundle\EasyAdminBundle\Dto\MenuItemDto;
use EasyCorp\Bundle\EasyAdminBundle\Router\AdminRouteGenerator;
use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
use Symfony\Component\HttpFoundation\Request;

Expand All @@ -17,6 +18,7 @@ class MenuItemMatcher implements MenuItemMatcherInterface
{
public function __construct(
private AdminUrlGenerator $adminUrlGenerator,
private AdminRouteGenerator $adminRouteGenerator,
) {
}

Expand All @@ -35,8 +37,7 @@ public function __construct(
*/
public function markSelectedMenuItem(array $menuItems, Request $request): array
{
$usePrettyUrls = true === (bool) $request->attributes->get(EA::ROUTE_CREATED_BY_EASYADMIN);
if ($usePrettyUrls) {
if ($this->adminRouteGenerator->usesPrettyUrls()) {
$menuItems = $this->doMarkSelectedPrettyUrlsMenuItem($menuItems, $request);
} else {
$menuItems = $this->doMarkSelectedLegacyMenuItem($menuItems, $request);
Expand Down
3 changes: 1 addition & 2 deletions tests/Controller/PrettyUrls/PrettyUrlsControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,15 +297,14 @@ public function testAdminUrlGenerator(): void
->setAction('customAction')
->generateUrl()
;
$client->request('GET', $url);
$client->request('GET', $url);

$this->assertSelectorTextSame('#url1', 'http://localhost/admin/pretty/urls?'.http_build_query([
'crudAction' => 'customAction',
'crudControllerFqcn' => CategoryCrudController::class,
'dashboardControllerFqcn' => DashboardController::class,
]));


$this->assertSelectorTextSame('#url2', 'http://localhost/admin/pretty/urls?'.http_build_query([
'crudAction' => 'customAction',
'crudControllerFqcn' => CategoryCrudController::class,
Expand Down
22 changes: 15 additions & 7 deletions tests/Menu/MenuItemMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use EasyCorp\Bundle\EasyAdminBundle\Config\Option\EA;
use EasyCorp\Bundle\EasyAdminBundle\Dto\MenuItemDto;
use EasyCorp\Bundle\EasyAdminBundle\Menu\MenuItemMatcher;
use EasyCorp\Bundle\EasyAdminBundle\Router\AdminRouteGenerator;
use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\HttpFoundation\InputBag;
Expand All @@ -19,7 +20,8 @@ public function testIsSelectedWhenContextIsNull()

self::bootKernel();
$adminUrlGenerator = self::getContainer()->get(AdminUrlGenerator::class);
$menuItemMatcher = new MenuItemMatcher($adminUrlGenerator);
$adminRouteGenerator = self::getContainer()->get(AdminRouteGenerator::class);
$menuItemMatcher = new MenuItemMatcher($adminUrlGenerator, $adminRouteGenerator);
$menuItemDto = new MenuItemDto();

$menuItemMatcher->markSelectedMenuItem([$menuItemDto], $request);
Expand All @@ -33,7 +35,8 @@ public function testIsSelectedWhenMenuItemIsSection()

self::bootKernel();
$adminUrlGenerator = self::getContainer()->get(AdminUrlGenerator::class);
$menuItemMatcher = new MenuItemMatcher($adminUrlGenerator);
$adminRouteGenerator = self::getContainer()->get(AdminRouteGenerator::class);
$menuItemMatcher = new MenuItemMatcher($adminUrlGenerator, $adminRouteGenerator);
$menuItemDto = new MenuItemDto();
$menuItemDto->setType(MenuItemDto::TYPE_SECTION);

Expand All @@ -50,7 +53,8 @@ public function testIsSelectedWithCrudControllers()

self::bootKernel();
$adminUrlGenerator = self::getContainer()->get(AdminUrlGenerator::class);
$menuItemMatcher = new MenuItemMatcher($adminUrlGenerator);
$adminRouteGenerator = self::getContainer()->get(AdminRouteGenerator::class);
$menuItemMatcher = new MenuItemMatcher($adminUrlGenerator, $adminRouteGenerator);
$menuItemDto = $this->getMenuItemDto();
$menuItemMatcher->markSelectedMenuItem([$menuItemDto], $request);

Expand Down Expand Up @@ -101,7 +105,8 @@ public function testIsSelectedWithRoutes()

self::bootKernel();
$adminUrlGenerator = self::getContainer()->get(AdminUrlGenerator::class);
$menuItemMatcher = new MenuItemMatcher($adminUrlGenerator);
$adminRouteGenerator = self::getContainer()->get(AdminRouteGenerator::class);
$menuItemMatcher = new MenuItemMatcher($adminUrlGenerator, $adminRouteGenerator);
$menuItemDto = $this->getMenuItemDto(routeName: 'some_route');

$menuItemMatcher->markSelectedMenuItem([$menuItemDto], $request);
Expand Down Expand Up @@ -142,7 +147,8 @@ public function testIsSelectedWithUrls()

self::bootKernel();
$adminUrlGenerator = self::getContainer()->get(AdminUrlGenerator::class);
$menuItemMatcher = new MenuItemMatcher($adminUrlGenerator);
$adminRouteGenerator = self::getContainer()->get(AdminRouteGenerator::class);
$menuItemMatcher = new MenuItemMatcher($adminUrlGenerator, $adminRouteGenerator);
$menuItemDto = new MenuItemDto();

$menuItemMatcher->markSelectedMenuItem([$menuItemDto], $request);
Expand Down Expand Up @@ -182,7 +188,8 @@ public function testMenuWithDashboardItem()

self::bootKernel();
$adminUrlGenerator = self::getContainer()->get(AdminUrlGenerator::class);
$menuItemMatcher = new MenuItemMatcher($adminUrlGenerator);
$adminRouteGenerator = self::getContainer()->get(AdminRouteGenerator::class);
$menuItemMatcher = new MenuItemMatcher($adminUrlGenerator, $adminRouteGenerator);
$menuItems = $menuItemMatcher->markSelectedMenuItem($menuItems, $request);

$this->assertSame('item2', $this->getSelectedMenuItemLabel($menuItems), 'Perfect match: Dashboard item');
Expand All @@ -197,7 +204,8 @@ public function testComplexMenu()

self::bootKernel();
$adminUrlGenerator = self::getContainer()->get(AdminUrlGenerator::class);
$menuItemMatcher = new MenuItemMatcher($adminUrlGenerator);
$adminRouteGenerator = self::getContainer()->get(AdminRouteGenerator::class);
$menuItemMatcher = new MenuItemMatcher($adminUrlGenerator, $adminRouteGenerator);

$menuItems = $menuItemMatcher->markSelectedMenuItem($menuItems, $request);

Expand Down

0 comments on commit 3aea6c9

Please sign in to comment.