6

I would like to change the comment/forum links to something like:

TOPIC:

#1

#2

#3

Do you have any idea on how should I do this?

4
  • Could you give an example on what you'd like it to look like? Jun 5, 2011 at 19:31
  • Sure - eu.battle.net/wow/en/forum/topic/2151761416 - at the right side the is #1,#2,#3 etc.
    – bart
    Jun 5, 2011 at 22:09
  • It's the same permalink of course but named properly (# + count).
    – bart
    Jun 5, 2011 at 22:28
  • Ah, I think I get it now. You're talking about displaying these links, not altering the actual links.
    – Berdir
    Jun 5, 2011 at 23:23

4 Answers 4

4

Advanced forum provides what you are looking for out of the box (a #number permalink in the upper left corner of the post) among tons of other useful features for a forum.

You could either simply use that module, or you could look at it's template files to see how it's done. The relevant code seems to be in includes/advanced_forum_preprocess_comment.inc and looks like this.

  /* Linked post number */
  if (!isset($post_number)) {
    static $post_number = 1;
  }

  $posts_per_page = variable_get('comment_default_per_page_' . $node->type, 50);

  $page_number = !empty($_GET['page']) ? $_GET['page'] : 0;
  if (!$page_number) {
    $page_number = 0;
  }

  $post_number++;

  $linktext = '#' . (($page_number * $posts_per_page) + $post_number);

  // Permalink
  //  You can erase next 3 lines if you wish to use built-in Permalink.
  //  Template adjusted: $post_link -> $permalink
      $uri = entity_uri('comment', $comment);
  $uri['options'] += array('attributes' => array('class' => 'permalink', 'rel' => 'bookmark'));
  $variables['permalink'] = l($linktext, $uri['path'], $uri['options']);
3
  • Does it work for an article comment as well ?
    – bart
    Jun 6, 2011 at 0:26
  • Advanced forum does it only for forum comments, but you can addapt it for all comments. You need to place that code in a comment preprocess function like adv_forum does and then use the $permalink variable in your template.
    – Berdir
    Jun 6, 2011 at 1:05
  • Is the if (!$page_number) { $page_number = 0; } part necessary? It would just set $page_number to 0, which is already done from the previous line.
    – apaderno
    Jul 18, 2015 at 10:07
3

First, if the code is in template_preprocess_comment(&$vars), $node->type needs to be $vars['node']->type. Also, there isn't the need of a comment counter per each page because the Comment module adds an ID starting with 1 for the top comment. Currently this code works correctly for me with comments on several pages.

function template_preprocess_comment(&$vars) {
  $posts_per_page = variable_get('comment_default_per_page_' . $vars['node']->type, 50);

  $page_number = !empty($_GET['page']) ? $_GET['page'] : 0;
  if (!$page_number) {
    $page_number = 0;
  }

  $linktext = '#' . (($page_number * $posts_per_page) + $vars['id']);

  $vars['comment']->uri['options'] += array('attributes' => array('class' => 'permalink', 'rel' => 'bookmark'));
  $vars['permalink'] = l($linktext, $vars['comment']->uri['path'], $vars['comment']->uri['options']);
}
3
  • If that works, then all the better, I know that there have been quite some improvements for comment permalinks in D7. Have you tested this with multiple pages of comments? Is the id always correct?
    – Berdir
    Jun 7, 2011 at 17:03
  • That's a good question. The problem is the ID# starts at one for each page which means page_1 which starts with the 11th comment will start numbering comments from #1. The top answer then is the correct answer to this problem.
    – Adam S
    Jun 7, 2011 at 18:00
  • this should be marked as answer
    – Alp
    Nov 22, 2011 at 16:31
2

OK got it to work properly ! In you template.php file (drupal/sites/adress/themes/YOURTHEME/template.php) add:

<?php 

function YOURTHEME!_preprocess_comment(&$variables){

  /* Easy links to the comment and parent node */
  $comment = $variables['comment'];
  $node = node_load($comment->nid);
  $variables['first_post'] = $node; 

  // Set the post ID for theming / targetting
  $variables['post_id'] = "post-$comment->cid";

  /* Linked post number */
  if (!isset($post_number)) {
    static $post_number = 1;
  }

  $posts_per_page = variable_get('comment_default_per_page_' . $node->type, 50);

  $page_number = !empty($_GET['page']) ? $_GET['page'] : 0;
  if (!$page_number) {
    $page_number = 0;
  }

  $post_number++;

  $linktext = '#' . (($page_number * $posts_per_page) + $post_number);

  // Permalink
  //  You can erase next 3 lines if you wish to use built-in Permalink.
  //  Template adjusted: $post_link -> $permalink
  $uri = entity_uri('comment', $comment);
  $uri['options'] += array('attributes' => array('class' => 'permalink', 'rel' => 'bookmark'));
  $variables['permalink'] = l($linktext, $uri['path'], $uri['options']);

}
?>
-1

You can type in your css file something like this:

.comment .submitted .permalink{ display: none; }

and no more permalink ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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