Allow users to delete their own comments

Public

Change the access callback for the "delete comment" menu item.

Get raw version
php
  1. /**
  2.  * Implements hook_menu_alter().
  3.  */
  4. function MY_MODULE_menu_alter(&$items) {
  5. $items['comment/%/delete']['access callback'] = 'MY_MODULE_comment_delete_access';
  6. $items['comment/%/delete']['access arguments'] = array(1);
  7. }

The new access callback for the "delete comment" menu item.

Get raw version
php
  1. /**
  2.  * New access callback for comment/%/delete - Allows to delete own comment.
  3.  */
  4. function MY_MODULE_comment_delete_access($cid) {
  5. global $user;
  6.  
  7. if (!user_is_logged_in()) {
  8. return FALSE;
  9. }
  10.  
  11. // Allow admin to delete any comments.
  12. if (user_access('administer comments')){
  13. return TRUE;
  14. }
  15.  
  16. // Load the comment.
  17. $comment = comment_load($cid);
  18.  
  19. // If user is comment author ..
  20. if ($comment->uid == $user->uid) {
  21. // .. check if comment mode for this node type is flat.
  22. $node = node_load($comment->nid);
  23. $comment_mode = variable_get('comment_default_mode_' . $node->type, COMMENT_MODE_THREADED);
  24. if ($comment_mode == COMMENT_MODE_FLAT) {
  25. // Comment mode is flat and user is author of the comment.
  26. return TRUE;
  27. }
  28. }
  29.  
  30. return FALSE;
  31. }