The comment_wrapper.tpl.php file does not provide logic to prevent outputting an empty wrapping div when no comments actually exist:

<div id="comments">
  <?php print $content; ?>
</div>

I am filing this as a support request because there may be a valid use-case that needs to be documented. If there is a good reason to output an empty div, please change this issue to the documentation category so we can find a place to explain this behavior.

This template's 5.x predecessor, theme_comment_wrapper(), also lacked logic. Here's an issue from the 4.7.x days that appears similar: [60589].

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Todd Nienkerk’s picture

Title: comment_wrapper.tpl.php outputs empty div when no comments exist » comment-wrapper.tpl.php outputs empty div when no comments exist
FileSize
354 bytes

Patch attached.

ainigma32’s picture

Status: Active » Needs review

Makes sense to me. Will try applying the patch when I get home.

- Arie

ainigma32’s picture

Status: Needs review » Needs work

Patch doesn't apply. You need to create the patch from the root of Drupal.

- Arie

JohnAlbin’s picture

subscribe.

Todd Nienkerk’s picture

Version: 6.9 » 6.10
FileSize
391 bytes

Rerolled patch to apply to root.

ainigma32’s picture

Well after this issue you're going to be start patch-maker :-)
Still does not apply. Why is there a mention of modules/comment/comment-wrapper.tpl.php.orig.php ?

Could you try one more time?

- Arie

Todd Nienkerk’s picture

ainigma: Thanks for bearing with me here. I've been writing patches for modules for months, but I've rarely done anything with core.

The mention of modules/comment/comment-wrapper.tpl.php.orig.php is the original comment-wrapper.tpl file. (The extra .php extension is annoying. I'm trying to do this while attending Drupalcon sessions, so I just ignored it.)

I used diff -up original.php new.php > patch.patch to create the patch. Should I be doing something differently, like cvs diff from a checkout? I don't normally mind CVS, but I'd rather avoid it if possible because I've never checked out core.

Again, sorry for the elementary how-to stuff. I thought I was far beyond this.

ainigma32’s picture

FileSize
614 bytes

No problem.

Personally I use Eclipse to create patches. I checkout Drupal using CVS and just create a patch using the UI.
So basically I'm just a mouse monkey clicking away :-D
What is important is that you use the Unified format (command line -u I think) and that you choose the drupal root as the root of the patch root. Of course using the UI I can easily select which changes I want to add to the patch and which I want to ignore. I suppose that's a little trickier using the commandline.

Result attached.

- Arie

Hope Murphy's law won't kick in now ;-)

ainigma32’s picture

Status: Needs work » Needs review

Hmmm thought I set this...

ainigma32’s picture

@Tod Nienkerk: Since I rolled the last one maybe you can test and review (and set to RTBC if it checks out) ?

- Arie

JohnAlbin’s picture

Version: 6.10 » 7.x-dev
Category: support » bug
Issue tags: +theme, +tpl-refresh

It looks RTBC, but we'll need to apply it to HEAD first. So let's have the testbot have a go at it. Then I'll mark it RTBC. :-)

[edit: Oh! And after that, we can apply it to DRUPAL-6, of course!]

Dries’s picture

I wonder if this is the right approach. Why do we even bother calling the theme system when there is no comment. Sounds like we're checking this at the wrong level. Should this check live somewhere in comment.module?

JohnAlbin’s picture

@Dries: very. good. point.

Here's a completely untested patch that will only call the theme function if there is content to wrap.

Todd Nienkerk’s picture

@JohnAlbin: I'm about to hop on a plane, so I can't brainstorm this at the moment... But can you think of any use cases where you would want to call the wrapper even if there were no comments? I suspect there may be some legitimate reasons.

Status: Needs review » Needs work

The last submitted patch failed testing.

JohnAlbin’s picture

Status: Needs work » Needs review
FileSize
876 bytes

Hmmm… with this patch applied, I was able to install HEAD and all the comments simple tests past. I have no clue why the patch "failed".

Re-rolled for no good reason.

Dries’s picture

Version: 7.x-dev » 6.x-dev
Status: Needs review » Reviewed & tested by the community

I ran the tests on my machine and everything is fine. Committed to CVS HEAD, and setting the version to Drupal 6.

JohnAlbin’s picture

Ok, I've re-rolled this patch for D6. Its a trivial re-roll, so I'm taking the liberty of leaving this as RTBC. :-D

I did verify the patch works on my D6 test system using Stark (for d6).

Dries’s picture

Status: Reviewed & tested by the community » Fixed

Committed to Drupal 6. Thanks! Marking this fix now.

Status: Fixed » Closed (fixed)

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

netsensei’s picture

Version: 6.x-dev » 7.x-dev
Status: Closed (fixed) » Active
FileSize
23.04 KB
865 bytes

Hm. My site is giving me this when there are no comments (screenshot attached)

'Reacties' is a h4 title inside comment-wrapper.tpl.php in my theme. Comments should be shown below. But there are no comments so it justs shows the h4 and goes on to render the comment form.

$content also contains the comment form box. So it can never really be empty (according to the patch). The code above the patch does that.

// If enabled, show new comment form if it's not already being displayed.
    $reply = arg(0) == 'comment' && arg(1) == 'reply';
    if (user_access('post comments') && node_comment_mode($nid) == COMMENT_NODE_READ_WRITE && (variable_get('comment_form_location_'. $node->type, COMMENT_FORM_SEPARATE_PAGE) == COMMENT_FORM_BELOW) && !$reply) {
      $output .= comment_form_box(array('nid' => $nid), t('Post new comment'));
    }

Instead of using $content, it would be better to use $num_rows since this variable is set to TRUE/FALSE depending on the presence of comments.

Attached to this comment my patch against HEAD.

JohnAlbin’s picture

Version: 7.x-dev » 6.x-dev
Status: Active » Closed (fixed)

@netsensei

Currently, the comment-wrapper.tpl.php doesn't have a title in it. It just has a single div which wraps the comments and the comment form. As such, the patch that was committed was sufficient to prevent an empty div from appearing on pages.

What you want is slightly different: 1. (possibly) have an H2 title in the comment-wrapper and 2.) not include that h2 title when there are no comments. I think that feature request actually dovetails nicely with this issue #517850: Update and polish default comment-wrapper.tpl.php and associated css, so you should head over there and leave this issue as closed.

dawansv’s picture

Old story but just want to second Todd Nienkerk (#14) comment... I was actually wondering why comment-wrapper was not running when there is no comment... No I see that's because of this patch. Not a big deal but indeed they are legitimate situations where one might want to run comment-wrapper even if they are no comments...

jmaties’s picture

Version: 6.x-dev » 7.x-dev
Status: Closed (fixed) » Active
FileSize
354 bytes

Hack comment module ;)

  • Dries committed 562f0ae on 8.3.x
    - Patch #372414 by Todd Nienkerk, JohnAlbin: don't output empty comments...

  • Dries committed 562f0ae on 8.3.x
    - Patch #372414 by Todd Nienkerk, JohnAlbin: don't output empty comments...

  • Dries committed 562f0ae on 8.4.x
    - Patch #372414 by Todd Nienkerk, JohnAlbin: don't output empty comments...

  • Dries committed 562f0ae on 8.4.x
    - Patch #372414 by Todd Nienkerk, JohnAlbin: don't output empty comments...

  • Dries committed 562f0ae on 9.1.x
    - Patch #372414 by Todd Nienkerk, JohnAlbin: don't output empty comments...