Hi all,

I was wondering how to customize the content part in page.tpl.php. I wanted to have an additional header '<h3>comments</h3> above the comment section, but this code print $content; outputs the entire content and I have no clue how to get to the different sections within the content var (like attachments & comments).

Of course I could use a quick 'n dirty hack for this particular need:

$content = str_replace('<a id="comment"></a>', '<a id="comment"></a><h3>Commentaar</h3>', $content);
print $content;

but surely there must be some better way to accomplish this.

Thanks!

Comments

pwolanin’s picture

Maybe you can ge the desired effect by overriding theme_comment_view in your theme?

---
Work: BioRAFT

appel’s picture

Thank you for your response. Can you give me a code example? I have no idea what to pass as arguments for example. And I would need to substitute $content for something else, right?

Thanks again!

pwolanin’s picture

Take a look at:

http://api.drupal.org/api/4.7/function/comment_render

in particular:

        if ($mode == COMMENT_MODE_FLAT_COLLAPSED) {
          $output .= theme('comment_flat_collapsed', $comment);
        }
        else if ($mode == COMMENT_MODE_FLAT_EXPANDED) {
          $output .= theme('comment_flat_expanded', $comment);
        }
        else if ($mode == COMMENT_MODE_THREADED_COLLAPSED) {
          $output .= theme('comment_thread_collapsed', $comment);
        }
        else if ($mode == COMMENT_MODE_THREADED_EXPANDED) {
          $output .= theme('comment_thread_expanded', $comment); 

So, depending on your comment settings you need to override one or all of these 4 theme function.

For example:

http://api.drupal.org/api/4.7/function/theme_comment_flat_expanded

function theme_comment_flat_expanded($comment) {
  return theme('comment_view', $comment, module_invoke_all('link', 'comment', $comment, 0));
} 

You could probably achieve want you want (note- I have NOT tested this) by putting in template.php for your theme something like:

function phptemplate_comment_flat_expanded($comment) {
  $output = "<div><h2>Comments on this post:</h2></div>";
  $output .= theme('comment_view', $comment, module_invoke_all('link', 'comment', $comment, 0));
  return $output;
} 

---
Work: BioRAFT

appel’s picture

Thank you for taking the time to help me out, really appreciate the detailed answer. Not there yet, but close. Your function phptemplate_comment_flat_expanded($comment) works to some extend, but it outputs 'Comments on this post:' before each individual comment block, while I (obviously) want the header to appear just once at the top of comments - if any.

I'm using 'comment_thread_expanded' mode and found the appropriate function over here: http://api.drupal.org/api/4.7/function/theme_comment_thread_expanded

function mythemename_comment_thread_expanded($comment) {
  $output = "<div><h2>Comments on this post:</h2></div>";
  if ($comment->depth) {
    $output .= '<div style="margin-left:'. ($comment->depth * 25) ."px;\">\n";
  }

  $output .= theme('comment_view', $comment, module_invoke_all('link', 'comment', $comment, 0));

  if ($comment->depth) {
    $output .= "</div>\n";
  }
  return $output;
}

Same problem with repeating header though. I figure I need to theme the parent to this function. Am I right?

Thanks again,

ap

pwolanin’s picture

Ok, maybe it can be easier than what I was suggesting.

Look at http://api.drupal.org/api/4.7/function/node_show

The comments are rendered after the entire node body is emitted. Thus, you could make your node.tpl.php look something like (adpating from bluemarine):

  <div class="node<?php if ($sticky) { print " sticky"; } ?>
    <?php if ($picture) {  print $picture;  }?>
    <?php if ($page == 0){ ?>
<h2 class="title"><a href="<?php print $node_url?>"><?php print $title?></a></h2>
    <?php }; ?>
    <span class="submitted"><?php print $submitted?></span>
    <span class="taxonomy"><?php print $terms?></span>
    <div class="content"><?php print $content?></div>
    <?php if ($links) { ?><div class="links">&raquo; <?php print $links?></div><?php }; ?>
<?php  if ($page && $node->comment){ ?>
<div><h2>Comments on this post:</h2></div>
<?php }; ?>
  </div>

I just tried this with my node-forum.tpl.php on a test site, and it seems to work!

---
Work: BioRAFT

appel’s picture

Thanks a bunch pwolanin, it works!

pwolanin’s picture

It seems $node->comment always has content. Still trying to figure that out.

---
Work: BioRAFT

pwolanin’s picture

<?php if ($page && $node->comment_count){ ?>
<div><h2>Comments on this post:</h2></div>
<?php }; ?>

prevents the header from showing if there are zero comments

OR:

<?php if ($page && $node->comment && $node->comment_count){ ?>
<div><h2>Comments on this post:</h2></div>
<?php }; ?>

seems $node->comment determines whether comments can be read/written for that node. So, without the check in the second set of code, the header will still show if comments were made but then comments for that node were disabled.

---
Work: BioRAFT

appel’s picture

Ok, so now it works :) Thanks a lot!

pwolanin’s picture

ok, added to the handbook as a theme snippet: http://drupal.org/node/76716

---
Work: BioRAFT

appel’s picture

Speaking of which, how does one add a snippet to the handbook?
Perhaps the answer to that could also be added to the handbook :)