Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all Comment ID's within a Drupal node

I have a node in Drupal with a few comments. Is there an easyish way to get the CID of every comment within the node? Also, is there a way to sort them by various parameters, chronology, karma of the comment etc. Thank you.

like image 550
alan Avatar asked Aug 31 '26 03:08

alan


2 Answers

I suppose you should check the comment_render function.

But if you need your own sort parameter, it'd be easier to do it using sql commands;

Check: http://api.drupal.org/api/function/comment_render/6

You can first make a query listing all the cid's on whatever you need to order;

$myquery = 'SELECT c.cid, c.pid, c.nid, c.subject, c.comment, c.format, c.timestamp, c.name, c.mail, c.homepage, u.uid, u.name AS registered_name, u.signature, u.picture, u.data, c.status FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.nid
= %d ORDER BY c.uid ASC';

$myresult = db_query($myquery)

This query exists on the comment_render function. But I tried to modify it for my use.

Now we have the node id and the cids in the order we wanted.

Here is the rendering work;

while ($mycomments = mysql_fetch_row($myresult)){

foreach ($mycomment as $mycid)

comment_render($nid, $mycid)

}

I haven't tested this one, but I hope it helps.

like image 164
Koray Al Avatar answered Sep 03 '26 06:09

Koray Al


You can load all comments for a node in drupal 7 using the function:

comment_get_thread($node, $mode, $comment_per_page)

Check out the documentation here: http://api.drupal.org/api/drupal/modules%21comment%21comment.module/function/comment_get_thread/7

It also discusses the default sort parameter. This does not however give you an easy way to resort the comments. I would just use views for that. Then you can use hook_node_view to disable the default comment display and add views_embed_view('my_view', 'my_display');

like image 22
danielson317 Avatar answered Sep 03 '26 08:09

danielson317



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!