Add ability to track active menu items in menu source

Created on 24 February 2025, 4 months ago

Problem/Motivation

Navigation components in Design systems often display differently active links (which link is same as current page). Example: menu, bottom navigation, ...

With menu source it's not possible, as in core block menu (boolean in_active_trail property), to differenciate active menu links.

Proposed resolution

Add a new information to store that information in getMenuItems() (MenuSource.php).

✨ Feature request
Status

Active

Version

2.0

Component

Code

Created by

πŸ‡«πŸ‡·France G4MBINI BΓ¨gles

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @G4MBINI
  • πŸ‡«πŸ‡·France G4MBINI BΓ¨gles
  • πŸ‡«πŸ‡·France G4MBINI BΓ¨gles
  • πŸ‡«πŸ‡·France G4MBINI BΓ¨gles

    @just_like_good_vibes I'll be happy to show you my issue with menus !

  • First commit to issue fork.
  • Merge request !363Set active menu trail β†’ (Open) created by mortona2k
  • πŸ‡ΊπŸ‡ΈUnited States mortona2k Seattle

    Loaded the menu.active_trail service to use getActiveTrailIds, and then $parameters->setActiveTrail().

  • Pipeline finished with Success
    3 months ago
    Total: 813s
    #464888
  • πŸ‡«πŸ‡·France Grimreaper France πŸ‡«πŸ‡·

    Should it be done in the menu source or in the links prop type?

    Currently the active trail is put as attribute in Links prop type, then I would say you play with that in your templates.

    In UI Suite Bootstrap, I kind of follow what core does with either JS for authenticated user or event subscriber for anonymous users (need the companion module in 🌱 [Discussion] blocked implementations and generic stuff Active )

  • πŸ‡ΊπŸ‡ΈUnited States mortona2k Seattle

    MenuSource is where the links are being loaded by MenuLinkTree->getMenuItems().

    That's where I adjusted the parameters being passed in.

    This is just like the default system menu block:
    https://git.drupalcode.org/project/drupal/-/blob/11.x/core/modules/syste...

    I recall finding out that active links are not the same as active menu trail. The active tab/local task is a good example of where .is-active is used. But I think active menu trail classes are generated by the menu tree, and don't need the js for anonymous users.

  • πŸ‡«πŸ‡·France Grimreaper France πŸ‡«πŸ‡·

    Hello,

    Stuff like item.in_active_trail is already possible.

    When using a pattern with menu source. Active trail is present but not set properly.

    Tested the MR and it fixes the issue.

    Thanks.

    Not merging because main UIP2 maintainers are now @just_like_good_vices and @christian.wiedemann, so maybe needs automated tests.

  • Pipeline finished with Failed
    2 months ago
    Total: 1328s
    #483932
  • Status changed to Needs work about 1 month ago
  • πŸ‡«πŸ‡·France Grimreaper France πŸ‡«πŸ‡·

    Changing to needs work, Tests in tests/src/Kernel/Source/MenuSourceTest.php should be completed I think.

  • πŸ‡«πŸ‡·France pdureau Paris
  • πŸ‡«πŸ‡·France pdureau Paris
  • πŸ‡§πŸ‡ͺBelgium svendecabooter Gent

    As reported in πŸ› [5.0.0-alpha3] Menu items do not get is-active class Active , this MR fixes the issue for me in ui_suite_daisyui for me, after adding some extra logic in the menu component of that theme.

    I think having active menu items is pretty basic expected behaviour for a theme, so would be good to get this included sooner rather than later. Having to patch both a module and theme to get a visual indicator of the active menu item, isn't the best situation for getting adoption IMHO. Therefor I took the liberty to increase the priority of this issue. Feel free to lower priority if you disagree.

  • πŸ‡«πŸ‡·France G4MBINI BΓ¨gles

    This issue as been tested 2 times by @Grimreaper and @svendecabooter and only needs tests now ...

    Do you think it could be added to 2.0.6 scope as it blocks some theme implementations ?

  • πŸ‡«πŸ‡·France just_like_good_vibes PARIS

    Hello,
    just to add some information about the current implementation, i am questioning here the way it is done,
    in comparison to the code in SystemMenuBlock::build

    we could use menuTree->getCurrentRouteMenuTreeParameters(,
    any thoughts?

  • Pipeline finished with Skipped
    about 23 hours ago
    #532515
Production build 0.71.5 2024