0

I tried to make StackExchange-like comment section with different template for the comment replies. I create two template for both top-level comment and its replies. This is the result of the two comment.tpl.php file rendered and then collated in GIMP.

Desired result

This is comment.tpl.php used for top comment contained in cyan box.

<div class="user-comment">
  <div<?php print $attributes; ?>>
    <div class="user-identification">
      <?
        print render($avatar) . "<br/>";
        print $commenter_name . "<br/>";
        print $creation_date;
      ?>
    </div>
    <?php
      hide($content['links']);
      print render($content['comment_body']);
      print render($content['links']);
    ?>
  </div>
</div>

While this is comment.tpl.php used for comment reply contained in yellow box.

<div class="user-comment">
  <div<?php print $attributes; ?>>
  <?php
    print render($content['comment_body']);
    print " - " . $commenter_name;
    print render($content['links']);
   ?>
  </div>
</div>

If i use one of these template, it will effect ALL the comments as shown below.

Reply to comment template

I know I can change the style rudimentarily via theme css as suggested in this question, but going by this route means that I have to create one-size-fit-all template for both case that won't be pretty.

My question is: Is there any template file I missed or any other strategy that I can employ to make separate template that will target only the replies-to-top-comment?

1 Answer 1

0

I manage to solve this problem by exploiting the pid (parent ID) value in the $comment variable. First, I check if the corresponding comment is a top-level comment. It is a top level comment if the pid value is 0. The code inside comment.tpl.php so far is:

if ($comment->pid == 0) {
  // Top level comments code here
} else {
  // Replies to comments code here
}

The code above will split the template programatically already, but it will be better if I worked at two separate files for the two cases. To do this, I used drupal_get_path function to point every case inside comment.tpl.php to their corresponding template.

In my case, I create these two templates inside my theme folder:

  • /templates/comment/comment-top.tpl.php for the top comment
  • /templates/comment/comment-reply.tpl.php for the replies

The code inside comment.tpl.php then become:

if ($comment->pid == 0) {
  include drupal_get_path('theme', 'mytheme') .
    '/templates/comment/comment-top.tpl.php';
} else {
  include drupal_get_path('theme', 'mytheme') .
    '/templates/comment/comment-reply.tpl.php';
}

Of course change the mytheme in the code to your theme name.

To finish off, I pasted the template I posted above in the question to the corresponding template. It will render the template properly with all the variables you define inside the preprocesses function working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.