2

When I click on the "Delete comment" link, I get redirected to a page that tells me to confirm the action. How can I avoid this redirection, as I have build a pop-up that asks for confirmation?

1 Answer 1

4

The comment/%/delete menu entry is wired directly to the comment_confirm_delete_page form. You can modify it with with a hook_menu_alter, and change the function from drupal_get_form to a page callback of your own design that will just delete the comment.

Example:

In your module file you'd need:

function mymodule_menu_alter(&$items) {
  $items['comment/%/delete']['page callback'] = 'my_comment_delete_function';
  $items['comment/%/delete']['page arguments'] = array(1);
  $items['comment/%/delete']['module'] = 'mymodule';
  $items['comment/%/delete']['file'] = 'mymodule.pages.inc';
}

And in your mymodule.pages.inc file you'd need:

function my_comment_delete_function($cid) {
  if ($comment = comment_load($cid)) {
    comment_delete($comment->cid);
    drupal_set_message(t('The comment and all its replies have been deleted.'));
    watchdog('content', 'Deleted comment @cid and its replies.', array('@cid' => $comment->cid));
    // Clear the cache so an anonymous user sees that his comment was deleted.
    cache_clear_all();

    drupal_goto("node/$comment->nid");
  }
  return MENU_NOT_FOUND;
}

Your Answer

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