Reputation: 1418
In my website I need to give a delete option for users to remove their own comment in a forum. I want to delete comments and update comment node statistics. Is there any Drupal functions available to delete comments?
Upvotes: 0
Views: 1675
Reputation: 29679
In Drupal 7 there is comment_delete_multiple() which invokes the hooks involved in the comments deletion, and updates the node statistics through _comment_update_node_statistics(). It requires an array of comment IDs.
In Drupal 6, there isn't an equivalent function, but you write the equivalent of the Drupal 7 function, considering that:
db_delete()
or comment_load_multiple()
Upvotes: 1
Reputation: 162
Drupal 7 Use the comment_delete()
<?php
//Delete single comment
$cid = 3917; //cid from `comment` table
comment_delete($cid);
//Delete multiple comments
$cids = array(137421,137426,137427,137428,137429,137430,137431,137434,137450,137472);
foreach ($cids as $cid) {
comment_delete($cid);
}
Upvotes: 0
Reputation: 579
Something like...
<?php
// you'll need to include /modules/comment/comment.admin.inc
module_load_include('inc', 'comment', 'comment.admin');
// I'm assuming you have access to the cid, the unique
// identifier for comments stored in the {comments} table
$comment = _comment_load($cid);
_comment_delete_thread($comment);
_comment_update_node_statistics($comment->nid);
?>
This solution is for Drupal 6 but you can borrow include files from the comment module and hijack their functions in other versions too.
I'm not sure if contrib modules extending the core forum functionality (like Advanced Forum) provide an option without writing custom code, but you might want to look into it.
Upvotes: 0