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.
Problem/Motivation
field--comments.html.twig uses as a condition {{ comments }} on the field title, however this is never empty afaict, there are cache tags, so the field title always shows, even for anon users, unless the title is explicitly hidden in field display settings.
Setting to major because this is broken template code - clearly the template is attempting to prevent this title from showing if there are no comments, but I assume somewhere along the line it was broken and no one noticed.
Proposed resolution
Find a condition to prevent it printing.
Remaining tasks
Fix it.
User interface changes
None
API changes
Probably none.
Comment | File | Size | Author |
---|---|---|---|
#33 | comment-title-2375981.test-only.patch | 1.38 KB | larowlan |
Comments
Comment #1
swentel CreditAttribution: swentel commentedSomething easy to get the day started.
This patch works.
Comment #3
yogen.prasad CreditAttribution: yogen.prasad commentedComment #5
Jeff Burnz CreditAttribution: Jeff Burnz commentedswentel included an extra condition, I haven't checked why we need this, maybe we need to elaborate why its needed.
Comment #6
kaustubhb CreditAttribution: kaustubhb commentedthis patch will solve problem of showing comment title even if the comment does not exist..
Comment #7
RavindraSingh CreditAttribution: RavindraSingh commentedPatch by @kaustubhb works as expected. Documentation is also added awesome. I have tested. See the screenshots.
If No comment
If comments
Comment #8
YesCT CreditAttribution: YesCT commentedThank you for the patch.
It was worked on during ghent, so let's leave that.
According to https://groups.drupal.org/node/447258 the tag should be SprintWeekend2015, no # sign.
Tags should be be separated with a comma, not a space.
And there is no tag #comment. The component is enough. Please do not make up new tags.
I'm not sure why the testbot did not pick up the patch. I'll try and request it report the results back to the issue.
Comment #9
prashantgoel CreditAttribution: prashantgoel commentedChanging the status might help test bot to pick and test the patch.
Comment #10
prashantgoel CreditAttribution: prashantgoel commentedPatch doesn't applies successfully. Requires reroll.
Comment #11
prashantgoel CreditAttribution: prashantgoel commentedSeeing if attached patch helps.
Comment #12
RavindraSingh CreditAttribution: RavindraSingh commentedFrom the submitted patch by @kaustubhb. Below line needs to be added as documentation.
Rest seems fine to me.
Comment #13
Jeff Burnz CreditAttribution: Jeff Burnz commentedI believe I have made a fundamental error in the OP (fixed now), probably because I was working with
node/1
and assumed the key1
was a numeric array key, however that key is the node ID.I've switched to doing this hack in my theme to make this work:
{% if comments['#pre_render'] and not label_hidden %}
FYI this is really just another instance of #953034: [meta] Themes improperly check renderable arrays when determining visibility
My apologies.
Comment #14
andypostThat's caused by #cache key that set in
\Drupal\comment\Plugin\Field\FieldFormatter\CommentDefaultFormatter::viewElements()
Comment #15
ameymudras CreditAttribution: ameymudras commentedComment #16
ameymudras CreditAttribution: ameymudras commentedThis patch fixed it for me. Rather than writing the condition based on {{ comments.1}} i have made use of {{ comments.pager}} because pager is set only when there are more than one comments.
Comment #17
ameymudras CreditAttribution: ameymudras commentedFind updated patch here
This patch fixed it for me. Rather than writing the condition based on {{ comments.1}} i have made use of {{ comments.pager}} because pager is set only when there are more than one comments.
Comment #18
andypostThat's could be tested that no "Comments" title displayed when there's no comments
Better to extend one of existing tests
\Drupal\comment\Tests\CommentInterfaceTest
Comment #19
ameymudras CreditAttribution: ameymudras commentedAdding the test as requested to check the comment title
Comment #20
ameymudras CreditAttribution: ameymudras commentedComment #21
andypostThe test passes probably because logged in user does not have the needed permission
Comment #22
andypostLet's add a test and test the patch with new test
Comment #24
ameymudras CreditAttribution: ameymudras commentedThis looks good now
Comment #25
Wim LeersI'm not sure we really want to introduce this hack. I think we actually want to fix #953034: [meta] Themes improperly check renderable arrays when determining visibility, which would fix this too?
Comment #26
Jeff Burnz CreditAttribution: Jeff Burnz commentedIndeed Wim, or we could use the render_var function? https://www.drupal.org/node/953034#comment-9537151
Comment #27
Wim LeersDoing that would mean you're no longer able to render individual children and then render the rest. So AFAIK it's only a work-around, not the recommended solution.
Comment #28
andypostsuppose we should add preprocess function to change "label_hidden" depending on presence of comment thread and comment form but taking into account field settings
Comment #29
andypostClosed as duplicate #2513068: Comments heading should not display when comments are closed and empty.
Comment #32
larowlanSo something else clearly resolved this.
Here's a re-roll of the test-only patch from #22 which now passes.
So I think new issue title and priority.
Comment #33
larowlanI can rtbc this, I only re-rolled #22
Couple of minor nit fixes
Comment #34
andypost+1 rtbc, looks twig now processes label of the field right
Comment #35
alexpottMoar tests. Committed f7f70ff and pushed to 8.0.x. Thanks!