Problem/Motivation
In Drupal 7, thanks to the implementation of l(), an "active" class was automatically added when generating node listing with views, and when the linked entity's URI was the same as the one listed.
With #1979468: ".active" from linkGenerator(), l() and theme_links() forces an upper limit of per-page caching for all content containing links, this is not the case any more and it's been reported to not be not easily alterable.
Proposed resolution
Re add the feature as optional, via means of adding a new checkbox form field to control whether or not the active class on links will be enabled. Adding of the actual CSS class is performed via JS.
Remaining tasks
Write the patchReview itAdd test coverage for the new feature.Write upgrade path to update existing views on sites.Write upgrade path test to verify it works.
User interface changes
New Set the active class on links checkbox on views field options. Description: If checked, if the fields link to the current page, an "is-active" class will be added on active links.

API changes
New set_active_class boolean on views.row.schema
Data model changes
N/A
Release notes snippet
Views can now optionally add the "active" css class.
Original description
In Drupal 7, thanks to the implementation of l(), an "active" class was automatically added when generating node listing with views, and when the linked entity's URI was the same as the one listed.
With #1979468: ".active" from linkGenerator(), l() and theme_links() forces an upper limit of per-page caching for all content containing links, this is not the case anymore and it seem's not easily alterable (I can't my way through).
Is there a way to change this behaviour. Are we missing something in core to let us alter that ?
| Comment | File | Size | Author |
|---|---|---|---|
| #159 | 2652000-10-4-159.patch | 38.01 KB | dmundra |
| #158 | 2652000-158.patch | 37.94 KB | dmundra |
| #154 | without_patch_applied_config_enabled_html.png | 40.97 KB | gabriel.passarelli |
| #154 | without_patch_applied_config_enabled_view.png | 27.17 KB | gabriel.passarelli |
| #154 | patch_applied_config_disabled.png | 39.27 KB | gabriel.passarelli |
Issue fork drupal-2652000
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
dawehnerIn D8 we have two mechanisms:
Comment #3
hazaThanks for the feedback Daniel.
I saw the 2 mechanisms while I was investigating on what was going on.
Here is a screenshot (left loggued as admin, right as anonymous, d-8.0.x).
The views (block) is displaying the nodes on the site, and the browser is on /node/2. That means (from what I understand) that we should have the "active" class on the "node 2" link.
Is there something missing on view's side ?
Comment #4
hazaComment #5
dawehnerMh, afaik views is not doing anything special here, maybe later I'll have a deeper look into it.
Comment #6
hazaHere is a try.
Since the generation of the "active" class is now an opt-in feature, I also made it as an opt-in option in views. I added a new option in the "Row style options" (since we want to add them when displaying fields)
Here is a screenshot of the option.

Using this option make views generate link fields (ie: the title of a node) with the "active" class if we are linking to the same page as the one currently displayed.
Comment #7
hazaComment #9
hazaComment #11
hazaOops, I forgot to update the schema. Here it is :)
Comment #12
hazaComment #13
dawehnerIn general I think this should NOT be an option on the row but rather on the field itself. This is more of a logical place to look for, when users would try to find it, IMHO.
Comment #14
duaelfr@dawehner Where in the field settings would you see this option ?
Comment #15
hazaI'm "just" uploading a new patch with a simple addition. It now also check for the addition of the active class if we rewrite the output of the link (displaying as link).
No change about the position of the settings right now. (thus leaving the status as "need work")
Comment #16
dawehnerI'm still a bit confused why we need that in views but not in other places in core. It sounds like a feature/setting we want for the normal link generation as well.
Comment #17
hazaI'm not sure to get your point dawehner. Since core set this option (
set_active_class) asFALSEby default, it is the responsability of each application that want to set this asTRUEto change the behaviour.And since we'd like to be able to generate active classes when using views, I guess this should be on views' side.
Otherwise, we could also say that we want to change the behaviour site's wise. There might be some place for a contrib module to do so.
Comment #19
mac_weber commentedChanging status. Lets see if test bot can get the latest patch.
Comment #20
mac_weber commentedoops, wrong status
Comment #23
simon.westyn commented@Haza, great patch! Thank you, it works perfectly.
Comment #24
willeaton commentedNice to have as a patch, but this was all 9 months ago. Has it not gone into core?
Comment #25
Pavan B S commentedRerolled the patch, please review
Comment #26
MerryHamster commentedCheking
Comment #27
MerryHamster commentedComment #28
MerryHamster commentedChecked the patch for 8.4-dev. Works fine.

Comment #29
andypostthis line should use short array syntax
Comment #30
gaurav.kapoor commentedComment #31
MerryHamster commentedFixed bugs with array syntax.
Comment #32
MerryHamster commentedComment #33
gaurav.kapoor commented@merryhamster : Thanks for patch in #28.suggestion in #29 were taken up in #30.
Comment #34
caldenjacobs commented#31 looks ready for prime time. Let's get this committed!
Comment #35
wim leers'Generate "active" class for links'Also: string addition, will need review.
s/in a link/on a link/
s/this views/this view/
The ternary opterator here can be removed.
Comment #36
csedax90 commented#31 works like a charm (I think it could be only a little bit cleaned) with 8.3.3. Could be ported on 8.3.4?
Comment #37
harsha012 commentedAs per #35 fixed the issue.
Comment #39
harsha012 commentedminor changes
Comment #41
manuel garcia commentedThis should fix the failing tests.
Comment #43
manuel garcia commentedLet's try that again.
Comment #44
Bojhan commentedThe only change I would make is go with "Add" and not "Generate".
Comment #45
manuel garcia commentedThanks @Bojhan, that makes sense. Doing that in this patch.
I've also changed the option to
add_active_classin order to keep things consistent in the code.Comment #46
csedax90 commentedOnly a question, if you group by a field (For example if you group all the products for their parent category and also this should be linked), the is-active class is correctly added to product when I'm inside it, but not to the grouped link if I come back to one level (I hope I explained myself)
Comment #47
c-logemannI successfully applied #45 patch on an actual 8.3.4 system and it works fine. So +1 for RTBC.
This feature is essential for me in D8 because I usually create dynamic menus with views and love this feature in D7.
Comment #48
caldenjacobs commented@C_Logemann — if you're building dynamic menus with Views, check out the little-known Link Trail by Path module as well!
This works as an alternative to patching Views—on almost all site content, not just Views—and also adds active-trail functionality:
https://www.drupal.org/project/link_trail_by_path
Comment #49
crtlf commentedPatch #45 works fine for me too, thank you !
Comment #50
manuel garcia commentedFixing a comment nitpick mentioned in #35.2 and changing my tests failure fix to be more in line with #35.4, which makes more sense.
Comment #52
pavelculacov commentedTested, worrked
Comment #53
caldenjacobs commentedComment #54
alexpottWe're missing test coverage for the new functionality. This is not a bug but by design. The active class handling was changed in Drupal 8 for performance. See https://www.drupal.org/node/2167077
Comment #55
firfin commented-- MY BAD: patch from #54 is working for me after all. Thanks, olafski
Original message:
Applying the patch manually to 8.4 doesn't seem to do anything. No active class added.
Is there some additional configuration / setting needed?
Comment #56
olafski@firfin, the patch in #50 worked for me on Drupal 8.4.2. To see the result I had to clear all caches after applying the patch.
Comment #58
xjmPlease only tag string changes after the issue is actually committed. Thanks!
Comment #59
bohart$item['rendered']can be an object of Markup, not a PHP array.This will cause a fatal error (that's happened for me).
Fixed the issue, patch attached.
Comment #60
andypost@bohart which kind of
MarkupInterfaceobject you get there?I'd say wont-fix because d8 set active class at front side & this change will break all caching in views
There's change record https://www.drupal.org/node/2167077
According that setting active class is set via
drupal.active-linklibrary https://cgit.drupalcode.org/drupal/tree/core/core.libraries.yml#n72The code is https://cgit.drupalcode.org/drupal/tree/core/misc/active-link.js so probably here some bug with views field plugins render of links
Or somehow theme or custom-whatever excludes library from pages - needs to dig
this will require to update all saved views! so needs upgrade patch and tests
Better to check that "rendered" is of renderable interface
very probably #url could be found in context of renderable element
Means need explicit tests for both cases
Comment #61
andypostChecked core usage of
system_page_attachments()is the only nowOn other hand this option could be useful at view level to attach core's
core/drupal.active-linkbut that affects whole page...So maybe better to name it kinda "Add view option to bubble up core/drupal.active-link library"
Not clear why that library works only for
data-drupal-link-system-pathdata attributeand only for menu links https://www.drupal.org/node/2281785
Or core hides this attribute from anonymous users for reason?
Comment #62
andypostmaybe js could filter somehow
window.locationof brower additionally to route naming for links - looks more markup relatedComment #63
dawehnerGiven that this results into the view being less cacheable, I'm wondering whether we could tell tha t the user somehow.
Comment #64
c-logemann@dawehner: Yes we should tell the user that this feature has an impact on caching.
I think it's enough to extend the description text with a warning.
Suggestion (also see patch and interdiff):
"If checked, if the fields link to the current page, an "is-active" class will be added on the link. WARNING: This feature limits the caching possibilities of this view and should only be enabled when needed."
Comment #66
chi commentedThere is a slight discrepancy here. Can we enclose the word "active" with quotes on the form element as well?
Comment #67
MerryHamster commentedReroll #64 patch for 8.7.x and added fix for #66.
Comment #68
MerryHamster commentedComment #69
andypostIt also needs upgrade path for existing views & tests (including cachability)
I think better to unify more - use the same name "set_active_class" for property
Comment #70
manuel garcia commentedComment #72
francoud commentedWell at the moment Views (D. 8.6.13) Views still doesn't add the "active" class.
The patch is still not applied.... Am I missing something...?
Comment #73
manuel garcia commented@francoud the patch is not applied to core yet is because (as of now):
After that and if there are no more concerns raised up by anyone, it can (hopefully) be marked as
Reviewed & tested by the communityat which point the core commiters will have a look at it, review it and if deemed acceptable, commit it.Comment #74
francoud commentedThanks Manuel Garcia.
Comment #75
olafskiNot tested systematically but patch #67 works for me on a Drupal 8.7.1 site.
Comment #76
plachThis addresses #69 and slightly tweaks the UI copy.
Comment #77
catchIs this actually true any more? The active class for links is set by misc/active-link.js. It does add some extra markup to the page though for that js, in https://api.drupal.org/api/drupal/core%21includes%21theme.inc/function/t...
Comment #78
aaronbaumanAre you asking whether the "WARNING" bit is still true?
I believe this is no longer applicable and should be removed.
Enabling "set_active_class" adds some overhead in LinkGenerator, but the rendered content should be totally cacheable since the class assignment is handled in javascript. IMO the additional processing cost is not worth mentioning in Views UI.
Attached patch updates the message.
Comment #79
manuel garcia commentedHappy to see progress on this!
I've updated the issue summary with the current status, etc.
Comment #80
timme77 commentedPatch in #78 works for me. Drupal 8.7.7
Comment #82
opiPatch in #78 works fine on Drupal 8.8.1
Comment #83
andypostClean-up implementation a bit but still needs tests and upgrades
Comment #84
andypostFurther clean-up
Comment #85
opiLatest patch in #84 works fine on Drupal 8.8.1, thanks for the update @andypost.
Comment #86
timme77 commentedI don't understand why the community doesn't include this in the core? Is there a way I can make it so that I don't have to apply the patch every time I update the Drupal 8 core?
Thx
Comment #87
aaronbauman@timme77 making changes to core takes significant time and effort.
the patch is on its way in to core, with several tasks remaining:
Write the patchBelive me, it's frustrating for everyone to have to wait so long on what seem like minor changes like this.
The sooner you or other volunteers can help out with these, the sooner the patch will land in core.
Comment #88
manuel garcia commentedI decided to have a go at the upgrade path & upgrade path test
I'm getting a schema error locally, however config_inspector does not seem to find any errors...
Schema key views.view.archive:display.default.display_options.row.options.set_active_class failed with: missing schemaAny pointers would be very welcome :)
Comment #90
manuel garcia commentedOK so the problem was that we only want to update the display configuration if:
- display uses fields
- display uses row options
Hopefully this will come back green now.
Comment #91
manuel garcia commentedComment #92
andypostGreat progress! The only missing is test
it may add a path to view to make sure that attribute is set in HTML
Comment #93
manuel garcia commentedThanks @andypost for the suggestion about the test, makes sense.
Rerolled #90, three-way merge did the trick:
Comment #94
manuel garcia commentedAdding coverage for the functionality itself.
I went with just adding it to the upgrade path test itself, to save the bot some time (and money). Let me know if we should move it to its own functional test instead.
Comment #95
andypostActive link class library has own functional tests, so here we just need to be sure that class is set and library attached, imo that's should be enough
Comment #96
manuel garcia commentedPatch #94 came out wrong due to incorrect git diff config on my end.
Here's the correct patch and the diff between them in case someone's curious.
Comment #97
manuel garcia commentedRe #95:
AFIK there is no need to check for the
core/drupal.active-linklibrary itself, as far as I can tell it is always added for authenticated users no matter what: https://git.drupalcode.org/project/drupal/blob/8.8.x/core/modules/system...I believe it doesn't hurt to have the extra coverage for when the current page is not the the page of the link and thus should not have the active link, so I'm leaving it in place :)
Comment #98
lendudeI like your thinking, but unfortunately it needs its own test. Update tests are removed when we remove the update itself (D10 probably) which would mean we would lose the coverage then.
hmmm the wording of the title makes it look like this would always be added. The description clarifies this, but we all know people don't read the UI, so can we clarify the title to make it clear this is added conditionally? Maybe just 'Set the active class on links to the current page', dunno, something like that.
The rest looks great !
Comment #99
manuel garcia commentedThanks @Lendude for the review!
Changing the option title here as suggested, I agree this clearer.
Also moving the functional test to its own actual functional test, and added a bit of coverage for the option in the views UI itself for good measure.
Comment #100
lendudeDove into this a little more, so more things I see:
Sorry I missed this the first time around but this sentence is a bit wonky. How about:
'When checked an "is-active" class will be added to all fields that are rendered as links that reference the current page.'
Hmm something like that.
We add the toggle for 'set_active_class' in two spots, but we are only testing one as far as I can see. So one lacks test coverage.
And then there is #13 which has not been addressed. I agree with @dawehner that the row settings are not a very logical place to add this option. If we feel that this IS the right place for this can we get some motivation as to why that would be? The setting isn't used in the row, it used at a field level.
Comment #101
manuel garcia commentedThanks again @Lendude for following up on this :)
First, the latest patch needed reroll due to #2989745: views_update_8500() inlines configuration changes instead of this being done on config save for bc
Comment #102
manuel garcia commentedRe #13 I think that makes sense, and also makes this feature more granular which is nice. I have moved it in this patch, which required adjusting the schema, update path and tests.
Also took the time to make use of the new
ViewsConfigUpdaterclass on the update path.The interdiff is very large mainly because I've updated core's default views (hopefully I didn’t miss any).
Comment #103
manuel garcia commentedSorry git diff is getting confused and generating strange patches. I regenerated the patch on #102 using
git diff -C95%and the new test views added should be easier to review.Comment #104
lendude@Manuel Garcia nice! Much more logical. Rewrite looks great.
The only real thing I'm concerned about is the placement. It is in prime real estate right now. I think it would be better to fold this into the 'rewrite results' fieldset, or maybe one of the others, but that one makes the most sense to me. Thoughts?
Comment #105
manuel garcia commentedThanks @Lendude for the review. I agree we should move it, I gave this some thought, and I believe the place to put this option is under Style settings. Since that is what CSS classes are used for, to me it makes sense to put it there. Doing so in this patch.
Comment #106
manuel garcia commentedgrr again git with the weird diffs, using
git diff -C95%for this one, interdiff on #105 is ok. Appologies..Comment #107
lendudeI'm happy, lets see what others think
Comment #108
manuel garcia commentedThanks @Lendude - just updating the issue summary a bit.
Comment #109
dwwMostly looks great, thanks! Big patch, but the actual change is quite small.
However, I've got a few nits / concerns before this is committed:
This description is rather clumsy:
A) The double "if X, if Y" at the start is a bit weird.
B) "Checked" seems potentially confusing.
C) "... will be added on active links." is weird. I thought we already qualified what links would see a change with "if the field links to the current page"...
How about:
'If the field links to the current page, an "is-active" class will be added if this option is enabled.'
Or something?
Maybe "set_active_class option" instead? With just spaces, it's a bit clumsy to parse.
And here... "Processes field set_active_class defaults."
A) What's "block" got to do with it? ;)
B) "set_active_class" again here?
Why do we care about sorts for this fixture?
And here?
Don't think the test is going to care about sorts, either. Generally, I'm in favor of less moving parts in any given test. If a test doesn't care, don't include that Views feature. Views tests are already complicated and bloated enough. ;)
set_active_class
$this->assertArrayHasKey('set_active_class', $field);$this->assertFalse($field['set_active_class');Enable the set_active_class option in the view.
Thanks!
-Derek
Comment #111
aleevas@dww thanks for your review.
I've added all your warnings and re-rolled the patch against 9.1 branch
Comment #112
aleevasSorry, was added a wrong interdiff.
Also I've made the patch for 8.9 branch with fixes
Comment #115
aleevasTrying to fix the failed tests.
The Interdiff the same for both patches
Comment #117
manuel garcia commentedFirst, thank you @dww for the review, all good points.
Also, thank you @aleevas for moving this forward.
I reviewed the changes since #106 and they look good to me. I attach an interdiff which includes all changes since then for easier review.
I noticed that the change in #109.11 were not quite right so fixing that here. The rest of points on #109 were addressed correctly.
Fixing the failing test here as well (hopefully)
Comment #118
manuel garcia commentedOops forgot the interdiff I talked about in #117...
Comment #119
dwwThanks for the fixes, @Manuel Garcia and @aleevas!
This is all I can find in #117 to complain about:
"we" would fit on the previous line. ;)
Definitely not worth re-rolling just for that, so I'm going to RTBC. But if anyone finds anything else that needs fixing, we could fix this, too.
Thanks again!
-Derek
Comment #120
alexpottWe need to trigger an E_USER_DEPRECATION error here. Something similar to:
@trigger_error(sprintf('The entity link url update for the "%s" view is deprecated in drupal:9.0.0 and is removed from drupal:10.0.0. Module-provided Views configuration should be updated to accommodate the changes described at https://www.drupal.org/node/2857891.', $view->id()), E_USER_DEPRECATED);And this message should be tested by the legacy test.
Comment #121
ramya balasubramanian commentedComment #122
ramya balasubramanian commentedHi @alexpott,
I have addressed your above comments. Please have a look and let me know if there are any issues.
Comment #123
ramya balasubramanian commentedHi @alexpott,
Since my previous patch test cases are failed, I am reuploading the patch with some changes. Please have a look.
Comment #125
ramya balasubramanian commentedFixing these test cases. Uploading the patch again.
Comment #126
ramya balasubramanian commentedFound some parameters are missing. Uploaded the patch again.
Comment #128
ramya balasubramanian commentedPlease have a look at this patch.
Comment #130
alexpottI think the number of deprecations here should make us step back and have a think. I have a couple of thoughts:
Comment #131
markusd1984 commentedCould this work/be made available for D7 as well?
I have the problem that many fields don't have the "active" class and thus hover effect styling is not possible
and therefore have to manually add this to each field every time (via style settings > customize field html > create a css class > "active").
Comment #132
c-logemann@markusd1984 This core issue is about loosing this possibility with views integration in D8 core. Now we have already D9 core stable and this is still not included. The better place to start is the Issue Queue of D7 Views Module. Maybe there is already an existing issue and hopefully a working patch for you. If not you can always open a new issue there.
Comment #136
gaëlgHere's a try to reroll #128.
Comment #137
manuel garcia commentedComment #138
gauravvvv commentedFixed PHPCS error for #136-9.3, Attached interdiff for same. Please review.
Comment #139
ranjith_kumar_k_u commentedComment #141
ryan-l-robinson commentedFollowing this thread. I'd love to see this incorporated into core as it is currently an issue for our site trying to style active links generated by a view.
Comment #143
prudloff commentedHere is a reroll of #139 for Drupal 9.4.1.
Comment #144
sauravkashyap commentedplease review my work
Comment #145
sauravkashyap commentedplease review my work
Comment #146
avpadernoComment #148
nod_Patch or MR doesn't apply anymore
The last patch or MR doesn't apply to the target branch, please reroll the code so that it can be reviewed by the automated testbot.
D10 version needed
At this time we would need a D10.1.x patch or MR for this issue.
careful reroll
The patch can be tricky to reroll and some code might get lost in the process. When rerolling this issue please make sure all the parts of the patch are kept (new files, new tests, all the changes to the different parts of the file, changes to es6 files ported to .js files, etc.)
Comment #149
jmsomera commentedHere is a reroll of #143 for Drupal 9.4.10.
Comment #150
avpadernoComment #151
_pratik_Reroll for 10.1.x
Please review
Thanks
Comment #154
gabriel.passarelli commentedPatch #151 worked for me:
Drupal: 10.1.1
PHP: 8.1.14
Before Patch
Views Configuration

HTML

After Patch
Views Configuration


HTML with config enabled
HTML with config disabled
Comment #155
jwilson3It is not entirely clear why the last patch in #151 failed because the Drupal CI job output is miles long. This needs to be converted to an MR, with "careful reroll" as pointed out in the issue tags, to get things over to GitLab CI.
Comment #158
dmundraCreating a patch file from the MR and uploading it here.
Comment #159
dmundraHere is a patch just for 10.4