0

On my site, moderators are notified of comments via email whenever a new comment is posted. I would like to offer them a one-click approve/delete feature directly from the email.

There is an approve comment link, so I can easily put that link into the email for them to approve the comment directly.

However comments are deleted via a form - an HTTP post - so I can't do that from a link in an email as far as I know. At least that's how it is on my site - not sure whether the form behaviour is normal or is because I have Mollom installed.

Is there a way to delete a comment directly from a link in an email?

1 Answer 1

0

This will do it.

Obviously you don't want to allow just anyone to delete any comment.

The authentication step is to require the user to be logged in with "administer comments" permission.

Alternatively, you could perhaps use a token in the querystring to check access to delete the comment.

/**
 * Implements hook_menu
 */
function MY_MODULE_menu() {

  $items['action/comment/%/delete'] = array(
    'title' => 'Delete comment',
    'page callback' => 'MY_MODULE_action_comment_delete',
    'page arguments' => array(2),
    'access arguments' => array('administer comments'),
    'type' => MENU_CALLBACK,
  );

  return $items;
}

/**
 * Menu callback
 * Delete comment via GET request
 */
function MY_MODULE_action_comment_delete($cid){

  try{
    // Delete the comment
    comment_delete($cid);
    drupal_set_message(t('The comment and all its replies have been deleted.'));
    watchdog('MY_MODULE', 'Deleted comment @cid and its replies.', array('@cid' => $cid));
    // Clear the cache so an anonymous user sees that his comment was deleted.
    cache_clear_all();
  }catch(Exception $e){
    drupal_set_message(t('There was a problem deleting the comment.'));
    watchdog_exception('MY_MODULE', $e);
  }

}

The path to delete a comment would then be:

http://www.example.com/action/comment/12/delete

Where "12" is the comment cid.

1
  • That looks good, thanks. I'll try it out and accept once I can. The comments are pre-moderated, they don't appear until they are accepted, so I think we won't need to clear the cache.
    – Fletch
    Commented Aug 28, 2013 at 14:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.