Comment Permalink

We encourage users to post events happening in the community to the community events group on https://www.drupal.org.
greggles's picture

I'd like to build a "permalink" module that gives consistent links to comments. Here are a couple of the requirements:

  • The permalink should be available on the comment itself
  • The permalink should stay the same regardless of where the comment is - that's particularly a problem on nodes with hundreds of active commenters and with a page limit (like 20 comments per page)
  • The permalink should go directly to the comment, through use of anchors in the url like #comment-CID
  • The permalink should show the comment within it's position on the node assuming the default rendering mechanism for that node type (i.e. some node types may have oldest first, some may have newest first, and some may use custom code to render comments in other ways) but this module will just use the core variables to determine where it is and go to that location for the comment

Prior work:

Plan

I plan to add to the permalink module, but wanted to post here first in case anyone else is aware of similar existing work. I'm aware that core for D7 has a similar feature.

Comments

You might want to talk to

Morbus Iff's picture

You might want to talk to Michelle; I coulda swore she did something in Advanced Forum related.

.

Michelle's picture

AF has two things. It has "#50" which is a link to the comment in context. It does this by figuring out what page the comment is on and adding an anchor link as well. I haven't tested, but I'm fairly sure this will only work on flat forums as I haven't put any logic in to account for comments moving around to different pages like they do in threaded forums. The other thing it has is a link to the Comment Page module's version of the comment.

Michelle

I did this on one forum-ish

Garrett Albright's picture

I did this on one forum-ish site, though I just did it through the theme layer; not really a module. And like the Permalink module, it was bugged once the thread exceeded one page of comments…

I just peeked through the source for Permalink. They seem to be using hook_link() to add the link to the comment, but nowhere in the code are they adding an anchor to the comment that I can find. I guess it's just sort of implicit that the theme is adding it itself, but not all themes are going to do that.

I think (consistently-working) comment permalinks will be a vital feature for discussion-oriented sites. I'm not really sure of another way to add an anchor to a comment other than the theme layer, though, seeing as there aren't any hooks to alter comments as they're loaded or viewed as there are with nodes. May I ask what your plan of attack on this is?

chx and I had a conversation

aaron's picture

chx and I had a conversation on irc once (a year ago, maybe?) about how it might be accomplished. disobey.com doesn't log #drupal, and i've long since lost the original log to a kernel upgrade gone bad...

what i recall was that we'd talked about a possible javascript solution, since drupal doesn't normally have access to a path's #anchor (which is the root of the current problem). though now that i think about it more, it might even be better to simply hook_link_alter to store the comment id in a query, along with the anchor, and that way you might be able to override the pagination accordingly.

not sure if that's helpful, but that's a bit of randomness that's sort of related... back to work for me...

Aaron Winborn
Drupal Multimedia (my book, available now!)
AaronWinborn.com
Advomatic

so yes, maybe this: alter

aaron's picture

so yes, maybe this:

  • alter the path of a permalink to be /node/nid?page=x&comment=cid#comment-cid
  • in hook_init, if you have $_GET['comment'] on /node/nid, then run a quick query and change $_GET['page'] if it exists (or set it if it isn't) to the proper page of the query

the rest should take care of itself. this assumes that $_GET['page'] is writable; I'm not sure offhand if it is.

feel free to ignore this if you already have a better system already in mind.

Aaron Winborn
Drupal Multimedia (my book, available now!)
AaronWinborn.com
Advomatic

for that matter, theme_pager

aaron's picture

for that matter, theme_pager already uses global variables, so you might be able to just change those instead of messing around with $_GET (which i'm sure is a bad idea). but hopefully i've rambled enough to get my idea out there.

Aaron Winborn
Drupal Multimedia (my book, available now!)
AaronWinborn.com
Advomatic

So how do we figure out what

Garrett Albright's picture

So how do we figure out what page an arbitrary comment is on?

Perhaps comment_new_page_count() could be used for this unintended purpose. It wants to know how many "new" comments there are in a thread, but maybe it'll work if we just find out how many comments for the node there are after the node in question and pass that as the $new_replies parameter. That way, comment_new_page_count() can manage the hairy matters about how the sort order or threaded/flat format of the comment affects things.

<?php
preg_match
('^node/(\d+)', $_GET['q'], $matches);
$node = node_load($matches[1]);
if (
$node) {
 
$fake_new = db_result(db_query('SELECT COUNT(cid) FROM {comments} WHERE nid = %d && cid > %d', $node->nid, $_GET['cid']));
 
$page = comment_new_page_count(comment_num_all($node->nid), $fake_new, $node);
  if (
$page !== $_GET['page']) {
   
drupal_goto($_GET['q'], "page={$page}&cid={$_GET['cid']}", "cid-{$_GET['cid']}", 301);
  }
}
?>

…Maybe?

AF's code

Michelle's picture

My response keeps getting flagged as spam without even the option of a captcha, so I pastebinned it: http://drupalbin.com/11357

Michelle

I have following solution for

mandarmbhagwat78's picture

I have following solution for your permalink module.

* The permalink should be available on the comment itself

Ans - As currently permalink module is for nodes you can create new module for comment 'permalink_comments'

* The permalink should stay the same regardless of where the comment is - that's particularly a problem on nodes with hundreds of active commenters and with a page limit (like 20 comments per page)

Ans - Every comment has a specific ID. My suggestion is to add a field into 'comments' table to set a flag and make it available to use it into the view. Thus when ever user mark a comment as a permalink, your module will update this flag field. Your module will create a view that will return all the comments marked as a permalink.

* The permalink should go directly to the comment, through use of anchors in the url like #comment-CID

Ans - In your view as you have set comment title as a link it will directly go the comment. No need to worry about $_GET parameters.

* The permalink should show the comment within it's position on the node assuming the default rendering mechanism for that node type (i.e. some node types may have oldest first, some may have newest first, and some may use custom code to render comments in other ways) but this module will just use the core variables to determine where it is and go to that location for the comment

Ans - See the above Ans. Also you can get the permalink's for a specific node or node type by using view arguments.

Thanks,
Mandar Bhagwat
+91 98224 39199
skype: mandar.bhagwat78

d7 style

greggles's picture

It appears most of the solutions suggested here won't work if they are used in e-mail or social bookmarking when comments are being pushed to different pages at a rapid pace. I looked at what core does for Drupal 7. The short answer is mostly in comment_permalink

  1. it creates a new page at comment/CID
  2. it does some dynamic calculation to figure out which "?page=" to be on
  3. it sets $_GET['q'] and $_GET['page'] to contain the right data
  4. it sets a canonical "link" element in the head that says to search engines "hey, this isn't the real page, the real one is over there" to remove duplicate content penalties

Seems like pure genius.

Seems like pure

Garrett Albright's picture

Seems like pure genius.

Thank you, because comment_permalink() calls comment_get_display_page() which in turn calls comment_get_display_ordinal() which is something of a rewriting/generalization of comment_new_page_count() as I suggested trying above. =]

When looking into the code, I

jcisio's picture

When looking into the code, I was asking why in step 4, they set canonical link to node/ID but not node/ID?page=PAGE. But in the latest revision (1.837), they'd removed the link. Don't know why.

IMHO, we could you Javascript in D6 to keep the url shorter. When we are at node/ID#comment-CID and we don't see the comment (client-side), we can use Ajax to fetch the correct comment page or redirect to the correct page (comment/CID#comment-CID).

Normally the comments per page is set at 50 or 100, and very few node have more than one comment page, so this kind of link works in 95% as the old style. It saves many many unnecessary comment/CID path in those nodes. With the 5% rest, we pay a redirect (or an Ajax call) and a full Drupal bootstrap, but I think the trade-off is ok. The Ajax way is more beautiful, even it breaks some modules like comment_display

The solution I suggested will

mandarmbhagwat78's picture

The solution I suggested will work as it returns original comment links with comment/CID to there is no possibility of duplication or writing any extra $_GET coding.

Thanks,
Mandar Bhagwat
+91 98224 39199
skype: mandar.bhagwat78

I had to implement something

dakku's picture

I had to implement something similar to the code above, hopefully this would help someone: http://janaksingh.com/blog/drupal-5-fix-comment-permalinks-pagination-85

The question is open in

andypost's picture

The question is open in related core issue https://drupal.org/node/2113323

Contributed Module Ideas

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

Hot content this week