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.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

swentel’s picture

Status: Active » Needs review
Issue tags: +Ghent DA sprint
FileSize
533 bytes

Something easy to get the day started.

This patch works.

Status: Needs review » Needs work

The last submitted patch, 1: 2375981-1.patch, failed testing.

yogen.prasad’s picture

Status: Needs work » Needs review
FileSize
794 bytes

Status: Needs review » Needs work

The last submitted patch, 3: 2375981-2.patch, failed testing.

Jeff Burnz’s picture

swentel included an extra condition, I haven't checked why we need this, maybe we need to elaborate why its needed.

kaustubhb’s picture

Issue tags: -Ghent DA sprint +#SprintWeekend2015 #comment
FileSize
1.02 KB

this patch will solve problem of showing comment title even if the comment does not exist..

RavindraSingh’s picture

Status: Needs work » Needs review
FileSize
19.25 KB
36.51 KB

Patch by @kaustubhb works as expected. Documentation is also added awesome. I have tested. See the screenshots.

If No comment
No comment

If comments

hide comment title

YesCT’s picture

Issue tags: -#SprintWeekend2015 #comment +SprintWeekend2015, +Ghent DA sprint

Thank 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.

prashantgoel’s picture

Status: Needs review » Needs work

Changing the status might help test bot to pick and test the patch.

prashantgoel’s picture

Issue tags: +Needs reroll

Patch doesn't applies successfully. Requires reroll.

prashantgoel’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
FileSize
517 bytes

Seeing if attached patch helps.

RavindraSingh’s picture

From the submitted patch by @kaustubhb. Below line needs to be added as documentation.

+++ b/core/modules/comment/templates/field--comment.html.twig
@@ -13,6 +13,7 @@
  * - title_suffix: Additional title output populated by modules, intended to
  *   be displayed after the main title tag that appears in the template.
  * - comments: List of comments rendered through comment.html.twig.
+ * - comments.1: to show the comment tile if comments exist comment.html.twig.

Rest seems fine to me.

Jeff Burnz’s picture

Issue summary: View changes
Status: Needs review » Needs work
Related issues: +#953034: [meta] Themes improperly check renderable arrays when determining visibility

I believe I have made a fundamental error in the OP (fixed now), probably because I was working with node/1 and assumed the key 1 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.

andypost’s picture

Issue summary: View changes
FileSize
12.75 KB

That's caused by #cache key that set in \Drupal\comment\Plugin\Field\FieldFormatter\CommentDefaultFormatter::viewElements()

ameymudras’s picture

Assigned: Unassigned » ameymudras
ameymudras’s picture

Status: Needs work » Needs review
FileSize
579 bytes

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.

ameymudras’s picture

FileSize
1.07 KB

Find 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.

andypost’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

That'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

ameymudras’s picture

Adding the test as requested to check the comment title

ameymudras’s picture

Status: Needs work » Needs review
andypost’s picture

Status: Needs review » Needs work

The test passes probably because logged in user does not have the needed permission

andypost’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests
FileSize
1.47 KB
2.54 KB

Let's add a test and test the patch with new test

The last submitted patch, 22: 2375981-test-only-22.patch, failed testing.

ameymudras’s picture

Assigned: ameymudras » Unassigned
Status: Needs review » Reviewed & tested by the community

This looks good now

Wim Leers’s picture

Status: Reviewed & tested by the community » Needs review

I'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?

Jeff Burnz’s picture

Indeed Wim, or we could use the render_var function? https://www.drupal.org/node/953034#comment-9537151

Wim Leers’s picture

Doing 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.

andypost’s picture

Status: Needs review » Needs work
+++ b/core/modules/comment/templates/field--comment.html.twig
@@ -27,7 +27,7 @@
-  {% if comments and not label_hidden %}
+  {% if comments.pager and not label_hidden %}

+++ b/core/themes/classy/templates/field/field--comment.html.twig
@@ -45,7 +45,7 @@
-  {% if comments and not label_hidden %}
+  {% if comments.pager and not label_hidden %}

suppose we should add preprocess function to change "label_hidden" depending on presence of comment thread and comment form but taking into account field settings

andypost’s picture

The last submitted patch, 22: 2375981-test-only-22.patch, failed testing.

larowlan’s picture

Title: Comments field title shows even when there are no comments » Test that Comments field title does not show when there are no comments
Priority: Major » Normal
Status: Needs work » Needs review
FileSize
1.39 KB

So 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.

larowlan’s picture

Status: Needs review » Reviewed & tested by the community
FileSize
1.38 KB

I can rtbc this, I only re-rolled #22
Couple of minor nit fixes

andypost’s picture

+1 rtbc, looks twig now processes label of the field right

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Moar tests. Committed f7f70ff and pushed to 8.0.x. Thanks!

  • alexpott committed f7f70ff on 8.0.x
    Issue #2375981 by ameymudras, andypost, larowlan, prashantgoel, yogen....

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.