Skip to content

Commit

Permalink
minor #5642 Improve the menu item highlighting (javiereguiluz)
Browse files Browse the repository at this point in the history
This PR was merged into the 4.x branch.

Discussion
----------

Improve the menu item highlighting

Fixes #5543.

Let's consider that a menu item pointing at 'index' action should be highlighted for all the actions of the same entity. This provides a better UX for end users.

Commits
-------

dbee3e4 Improve the menu item highlighting
  • Loading branch information
javiereguiluz committed Feb 20, 2023
2 parents 0661f6a + dbee3e4 commit 8c2d8ee
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/Menu/MenuItemMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace EasyCorp\Bundle\EasyAdminBundle\Menu;

use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use EasyCorp\Bundle\EasyAdminBundle\Config\Option\EA;
use EasyCorp\Bundle\EasyAdminBundle\Dto\MenuItemDto;
use EasyCorp\Bundle\EasyAdminBundle\Provider\AdminContextProvider;
Expand Down Expand Up @@ -33,8 +34,9 @@ public function isSelected(MenuItemDto $menuItemDto): bool
return $menuItemDto->getLinkUrl() === $adminContext->getRequest()->getUri();
}

$menuItemQueryParameters = $this->filterIrrelevantQueryParameters($menuItemQueryParameters);
$currentPageQueryParameters = $this->filterIrrelevantQueryParameters($currentPageQueryParameters);
$menuItemLinksToIndexCrudAction = Crud::PAGE_INDEX === ($menuItemQueryParameters[EA::CRUD_ACTION] ?? false);
$menuItemQueryParameters = $this->filterIrrelevantQueryParameters($menuItemQueryParameters, $menuItemLinksToIndexCrudAction);
$currentPageQueryParameters = $this->filterIrrelevantQueryParameters($currentPageQueryParameters, $menuItemLinksToIndexCrudAction);

// needed so you can pass route parameters in any order
sort($menuItemQueryParameters);
Expand Down Expand Up @@ -63,10 +65,20 @@ public function isExpanded(MenuItemDto $menuItemDto): bool
* should be ignored when deciding if some menu item matches the current page
* (such as the applied filters or sorting, the listing page number, etc.).
*/
private function filterIrrelevantQueryParameters(array $queryStringParameters): array
private function filterIrrelevantQueryParameters(array $queryStringParameters, bool $menuItemLinksToIndexCrudAction): array
{
$paramsToRemove = [EA::REFERRER, EA::PAGE, EA::FILTERS, EA::SORT];

// if the menu item being inspected links to the 'index' action of some entity,
// remove both the CRUD action and the entity ID from the list of parameters;
// this way, an 'index' menu item is highlighted for all actions of the same entity;
// however, if the menu item points to an action different from 'index' (e.g. 'detail',
// 'new', or 'edit'), only highlight it when the current page points to that same action
if ($menuItemLinksToIndexCrudAction) {
$paramsToRemove[] = EA::CRUD_ACTION;
$paramsToRemove[] = EA::ENTITY_ID;
}

return array_filter($queryStringParameters, static fn ($k) => !\in_array($k, $paramsToRemove, true), \ARRAY_FILTER_USE_KEY);
}
}

0 comments on commit 8c2d8ee

Please sign in to comment.