Clear ‘cache_page’ once new node comment has been saved in Drupal 7

Slightly surprised this hasn’t been covered anywhere before. Building upon the answer to a similar problem in Drupal 5, I thought it would be beneficial to post how to achieve the same thing in Drupal 7.

Since hook_comment is now unavailable in Drupal 7 (in favour of some more explicit interception-based hooks), I’ve decided to utilise hook_comment_insert for this purpose. Together with a revised url() syntax, the hook looks as follows:

function hook_comment_insert($comment) {
// Clear page-specific cache after comment has been saved for that page
$url = url('node/' . $comment->nid, array('absolute' => TRUE));
cache_clear_all($url, 'cache_page');
}

Comments are closed.