开发者

How do I programmatically delete forum comments in drupal?

开发者 https://www.devze.com 2023-03-25 13:38 出处:网络
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 sta开发者_JAVA百科tistics. Is there any Drupal function

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 sta开发者_JAVA百科tistics. Is there any Drupal functions available to delete comments?


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:

  • Drupal 6 doesn't have functions to handle transactions
  • In Drupal 6, the name of hooks invoked when deleting a comment are different
  • Drupal 6 doesn't have entity fields
  • Drupal 6 doesn't have any equivalent for db_delete() or comment_load_multiple()


http://drupal.org/documentation/modules/trigger

Drupal trigger.


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.


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);    
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号