function hook_comment

You are here

4.6 core.php hook_comment($op, $comment)
4.7 core.php hook_comment($a1, $op)
5 core.php hook_comment(&$a1, $op)
6 core.php hook_comment(&$a1, $op)

Respond to comment actions.

This hook allows modules to extend the comments system by responding when certain actions take place.

Parameters

$a1: Argument; meaning is dependent on the action being performed.

  • For "validate", "update", and "insert": an array of form values submitted by the user.
  • For all other operations, the comment the action is being performed on.

$op: The action being performed. Possible values:

  • "insert": The comment is being inserted.
  • "update": The comment is being updated.
  • "view": The comment is being viewed. This hook can be used to add additional data to the comment before theming.
  • "validate": The user has just finished editing the comment and is trying to preview or submit it. This hook can be used to check the comment. Errors should be set with form_set_error().
  • "publish": The comment is being published by the moderator.
  • "unpublish": The comment is being unpublished by the moderator.
  • "delete": The comment is being deleted by the moderator.

Related topics

7 functions implement hook_comment()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

chameleon_comment in themes/chameleon/chameleon.theme
comment_invoke_comment in modules/comment/comment.module
Invoke a hook_comment() operation in all modules.
search_comment in modules/search/search.module
Implementation of hook_comment().
template_preprocess_comment in modules/comment/comment.module
Process variables for comment.tpl.php.
theme_comment in developer/theme.php
Implemented using the comment.tpl.php template.

... See full list

5 invocations of hook_comment()
comment_invoke_comment in modules/comment/comment.module
Invoke a hook_comment() operation in all modules.
comment_render in modules/comment/comment.module
Renders comment(s).
hook_search in developer/hooks/core.php
Define a custom search routine.
theme_comment_flat_expanded in modules/comment/comment.module
Theme comment flat expanded view.
theme_comment_thread_expanded in modules/comment/comment.module
Theme comment thread expanded view.

File

developer/hooks/core.php, line 270
These are the hooks that are invoked by the Drupal core.

Code

function hook_comment(&$a1, $op) {
  if ($op == 'insert' || $op == 'update') {
    $nid = $a1['nid'];
  }

  cache_clear_all_like(drupal_url(array('id' => $nid)));
}

Comments

the 'unpublish' $op doesn't actually exist. Which makes total sense, since the 'publish' $op does exist... wait... what?

A possible workaround is to use 'update', and check the value of the comment's status, to determine if has just been unpublished.

If you're publishing/unpublishing from the comment/edit/* page, the $a1 in hook_comment() is going to be an array, but if you're doing that from the admin/content/comment/approval page, $a1 is actually the comment object.

Hope this saves someone else a few hours.

Apparently this doc page isn't even remotely accurate.

http://drupal.org/node/314138#comment-4687412

Seriously, this caused me some frustration. Read the discussion at the link above. I finally got my implementation of hook_comment() working by using the parameter &$comment instead of &$a1 like they did in user_comment().

Any substitutions for D7?

Hi,

I wand to do some changes in comment body before comment save (need to replace some text in comment). I am trying to add this:

switch ($op) {
case 'insert':
$comment['comment'] = changeText($comment['comment']);
break;
}
But changes are not reflecting after comment save. However 'view' is working properly. I am working on drupal 6.

Any idea?

The changes were not saved because the $op 'insert' is called after the comment is saved on the database. What you can do in order to do what you want is call comment_save() with the updated text inside your hook implementation