Question

I want to theme the "Add Comment" and "Comments" links that are shown on a node that has comments enabled. I know there's theme() and theme_links() that can help with this, but I'm not sure how to use them. I'm pretty sure I want theme_links(), since I'm after links in this case. But how to I get the comment links specifically? I don't want to theme all links, just the ones on the comments. If it helps, my goal is to add an image next to each of these links. Also, next to "Comments" I want to include the number of comments posted.

To clarify, I want to theme the links that appear on the Node, not the links that appear on the Comments themselves.

Was it helpful?

Solution

To add an image/icon to a link, you can use simple CSS. This CSS will add an icon to the "Add Comment" link, but the same could be done for other links as well (li.comment_delete, li.comment_edit, etc).

ul.links > li.comment_add > a {
  background: url(PATH TO IMAGE) no-repeat;
  padding-left: 20px;  /* Change to compensate for size of image */
}

To add the number of comments on a node you can use the function comment_num_all($node->nid). For instance if you would like to add the number of comments to the "Add comment" link, you could add a hidden DIV to the node.tpl.php (or each content type template) and jQuery to edit the link text:

<div id="num-comments" style="display:none;"><?php print comment_num_all($node->nid); ?></div>

jQuery:

$('ul.links > li.comment_add > a').text('Add new comment (' + $('#num-comments').text() + ')');

This is not the most elegant solution, but it works. If you want to use theme_links() I think you would have to create a custom module.

EDIT: Another option is to create a custom module. This does not use theme_links(), but hook_link_alter() instead. This is a small example module to change the title of the "Add new comment" link, add an icon and include the number of current comments attached to the node: (Replace every instance of MYMODULE_NAME with the name you choose for the module)

STEP 1: Create a file called MYMODULE_NAME.info and add:

name = "MYMODULE_NAME"
description = "Change the appearance of links that appear on nodes"
core = 6.x

STEP 2: Create file called MYMODULE_NAME.module and add:

<?php

  /**
   * Implementation of hook_link_alter
   */
  function MYMODULE_NAME_link_alter(&$links, $node){
    if (!empty($links['comment_add'])) {
      // Get number of comments for node
      $num_comments = db_result(db_query('
        SELECT comment_count 
        FROM {node_comment_statistics} 
        WHERE nid = %d
      ', $node->nid));

      // Set "Add new comment" link text
      $links['comment_add']['title'] = '<img src="PATH TO ICON"/> ADD COMMENT TEXT (' . $num_comments . ')';

      // Allow HTML in the link text
      $links['comment_add']['html'] = TRUE;
    }
  }

STEP 3: Put these files in a folder called MYMODULE_NAME, place the folder in sites/all/modules, and enable the module

EDIT: To find array keys: in your node.tpl.php (or any other node template) you can add <?php print_r($node->links); ?>. This will show you all of the link info to be displayed in the node, and the keys of the main array are what you would use in my module. You can also try using Firebug/Chrome Dev Tools, etc to look at the class of the list item containing the link (ie ul.links > li.comment_add). I believe when the links are constructed, Drupal uses the array key as a class for the link.

OTHER TIPS

I think that the simplest thing to do would be to override the comment.tpl.php file in your theme. You can copy the one in /themes/garland to use as a base.

I've used this technique from http://drupal.org/node/352020 for creating the same kind of links, you want to add a preprocess hook to your module to access the $links array:

function yourmodule_preprocess_comment (&$variables) {
  $comment = $variables['comment'];

  //load links for current comment
  $links = comment_links($comment, FALSE);

  //code to alter the links array

  //reset the links HTML
  $variables['links'] = theme('links', $links);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top