When using #theme => links, you can set the 'set_active_class' option and get the CSS class is-active added to both the LI and the A element when the link's URL matches the current URL.
However, if the URL in question contains a query, the is-active class only gets added to the A element, and not the LI.
This is because template_preprocess_links() is not correctly getting the query parameters from the URL:
// Add a "data-drupal-link-query" attribute to let the
// drupal.active-link library know the query in a standardized manner.
if (!empty($link['query'])) {
$query = $link['query'];
ksort($query);
$li_attributes['data-drupal-link-query'] = Json::encode($query);
}
The query is no longer in $variables['links']['query'], but part of the URL object passed in $variables['links']['url'].
| Comment | File | Size | Author |
|---|---|---|---|
| #28 | 2726471-28.patch | 1.84 KB | duaelfr |
| #21 | Screenshot 2020-07-28 at 8.21.43 AM.png | 33.84 KB | samiullah |
| #21 | Screenshot 2020-07-28 at 8.18.06 AM.png | 126.02 KB | samiullah |
| #20 | interdiff_17-20.txt | 987 bytes | vsujeetkumar |
| #20 | 2726471_20.patch | 1.84 KB | vsujeetkumar |
Comments
Comment #2
joachim commentedThe 'query' property is on the URL that's set in 'url', not on the link array itself.
Comment #3
joachim commentedUpdated patch. Previous patch was still looking in $links['query'], which is obsolete.
Comment #4
zerolab commentedUsing this patch in production without any issue.
Code looks sound.
Thank you
Comment #5
xjmThanks @zerolab and @joachim!
Looks like we could use some test coverage for this bug?
Comment #6
smazI assume the test will need to be in core/modules/system/src/Tests/Theme/FunctionsTest.php, as FunctionsTest::testLinks() & FunctionsTest::testIndexedKeyedLinks() check the results of links.html.twig.
If that's the case, and no one gets to this, I'll try and take a look.
Cheers
Comment #9
mpp commentedThe patch doesn't solve the issue of links that aren't highlighted when there's a query parameter in the url of the current page.
Comment #16
rgnyldz commentedThe patch on # is not applying to 8.9.2
The line is now on 730 in file /core/includes/theme.inc
And the issue is still here on D8
Comment #17
vsujeetkumar commentedRe-roll patch for 9.1.x, Please review.
Comment #18
rgnyldz commentedPatch is applying for 8.9.2 but the issue still persists.
For the issues sake I'll test it on a clean D9 and get back with feedbacks soon.
Comment #20
vsujeetkumar commented@RgnYLDZ thanks for replying, Waiting for your feedback.
I also fixed one failed test, Please have a look and advise.
Comment #21
samiullah commented@vsujeetkumar
Applied the patch on the clean install of drupal 9.1.x
Was able to see active class appending to links with query
This can be moved for RTBC
Before: https://www.drupal.org/files/issues/2020-07-28/Screenshot%202020-07-28%2...
After: https://www.drupal.org/files/issues/2020-07-28/Screenshot%202020-07-28%2...
Comment #22
samiullah commentedComment #23
lauriiiI'm wondering if the fact that we need to fix the assertion in the failing test means that something was broken because the test documents the following assumption:
It would be good to also have explicit test coverage for this.
Comment #25
gwvoigt#20 fails on 9.1.6Never mind, I guess my issue was not related to this one.
Comment #28
duaelfrJust a reroll for 9.4.x to see if it still works.
Comment #29
carsteng commentedComment #34
duaelfr