Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix menu item matcher when using pretty URLs and submenus #6625

Merged
merged 1 commit into from
Dec 7, 2024

Conversation

valkars
Copy link

@valkars valkars commented Dec 5, 2024

If menu has submenus, then opening any submenu item triggers "selecting" of all other submenus first item besides opened menu item. For example:

  • Menu item 1
  • Submenu 1
  • Submenu item 1-1
  • Submenu item 1-2
  • Submenu 2
  • Submenu item 2-1
  • Submenu item 2-2
  • Submenu item 2-3
  • Submenu 3
  • Submenu item 3-1
  • Submenu item 3-2

Click on Submenu item 2-2 and these items will be selected: Menu item 1, Submenu item 1-1, Submenu item 2-2, Submenu item 3-1.
Admin url is localhost/, submenu url is localhost/banana, so in MenuItemMatcher method doMarkSelectedPrettyUrlsMenuItem $currentRequestUriWithoutAction will be an empty string:

// the edit URL is '/admin/foo/{id}/edit' so we need to remove the two last segments of the URL
if (str_ends_with($currentRequestUriWithoutQueryString, '/edit')) {
    $currentRequestUriWithoutAction = preg_replace('#/[^/]+$#', '', $currentRequestUriWithoutAction);
}

Later this condition will be true and item will be selected, because str_ends_with('any-string', '') - returns true.

// compare the ending of the URL instead of a strict equality because link URLs can be absolute URLs
if (str_ends_with($menuItemUrl, $currentRequestUriWithoutQueryString)
    || str_ends_with($menuItemUrl, $currentRequestUriWithoutAction)
    || str_ends_with($menuItemUrlWithoutQueryString, $currentRequestUriWithoutQueryString)
    || str_ends_with($menuItemUrlWithoutQueryString, $currentRequestUriWithoutAction)) {
    $menuItemDto->setSelected(true);

So, I've added check for an empty string and then menu selecting is correct: only Submenu item 2-2.

@javiereguiluz
Copy link
Collaborator

@valkars I had this in my TODO list. Thanks a lot for taking care of this. I'm going to review it quickly and I'll try to merge it soon. Thanks!

@javiereguiluz javiereguiluz added this to the 4.x milestone Dec 5, 2024
@javiereguiluz
Copy link
Collaborator

It's merged! Thanks a lot @valkars

@javiereguiluz javiereguiluz merged commit f7c0915 into EasyCorp:4.x Dec 7, 2024
12 checks passed
@valkars valkars deleted the hotfix/menu-matcher branch December 8, 2024 13:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants