Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
This was a request from one of the theme devs working on the Twig conversion. I'm moving it to a feature request here so I can take the TODO out of our template file.
I believe active links already get a special class, so we may end up using the active class that's there already for this purpose (so marking the issue as minor)
Comments
Comment #1
c4rl CreditAttribution: c4rl commentedAltering title slightly
Comment #2
c4rl CreditAttribution: c4rl commentedI have a daring proposal:
What if this were part of ArrayAccess::offsetSet? Meaning, when href was set, it would run the .active class check and if it were the case, call $this->offsetSet('class', 'active');
Comment #3
c4rl CreditAttribution: c4rl commented:) Potential issue with daring proposal in #2
Looking at l(), since the .active class is checked against $path and href is set to url($path, $options), there would need to be a way to clarify this. Checking against a static set in url() is a potential solution.
Comment #4
sunI have troubles to understand this issue, as I don't see a concrete feature request for what is actually wanted anywhere.
"Create an .active CSS class on a link (A) element based on its attributes" is all what I got so far, but that makes 0% sense.
A link gets .active if its href matches the currently requested page. If it doesn't have an href, it cannot get active.
The .active class is specific to the current page and not to be mistaken with the .active-trail class, which is special to links appearing in menu link trees.
Comment #5
c4rl CreditAttribution: c4rl commentedI can see what happens when I am up too late in the issue queue. :) Wild ideas.
I thought about this some more and discussed with jenlampton, and it seems for the majority of use cases, having a .active class on a link only seems pertinent to navigational items, where the given link in the should change in appearance. Indeed, this is something we've had in Drupal a long time, but it's traditionally been part of the l() function, not part of menu theming. Given all the related discussion about l() and theme links, I'm going to mark this as postponed as we may discover that refactoring menu theming to add the .active class instead of l() is a way forward.
Comment #6
thedavidmeister CreditAttribution: thedavidmeister commentedMaking the title reflect more accurately what (I think) is being asked. l() already handles the .active class. Correct me if I'm wrong but I think the intention here is really to make sure that theme_link() (if not overridden by the theme system) produces the same output for l() consistently if it receives the same input.
That way the changes proposed in #1778610: Remove the check for a link template from l(), have l() always output just a string. become much more palatable.
Comment #7
thedavidmeister CreditAttribution: thedavidmeister commentedThis is now a duplicate with #1778616: Add an .active class for links rendered by theme('link') that have an href to the URL being viewed and sanitize output of url(). Even though this issue is older, that one has a patch that's looking close to RTBC.