Hi,
the docs for hook_comment() are missing all required return values for the different $op's.
Also, the example code is wrong, it should move the
cache_clear_all_like(drupal_url(array('id' => $nid)));
into the if clause because else $nid is undefined, and it makes no sense at all. Also, this function doesn't return at all (I don't know if the hook system can handle this)

CommentFileSizeAuthor
#3 314138-hook_comment_docs.diff1 KBjeffschuler

Comments

lilou’s picture

Title: hook_comment()-Documenattion is missing infos » hook_comment() documentation is wrong
Category: bug » task

Example code is : http://api.drupal.org/api/function/hook_comment/7

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

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

cache_clear_all_like() doesn't exist.

Example implementation : http://api.drupal.org/api/function/user_comment/7

function user_comment(&$comment, $op) {
  // Validate signature.
  if ($op == 'view') {
    if (variable_get('user_signatures', 0) && !empty($comment->signature)) {
      $comment->signature = check_markup($comment->signature, $comment->format);
    }
    else {
      $comment->signature = '';
    }
  }
}

Other example implementation : http://api.drupal.org/api/function/search_comment/7

function search_comment($a1, $op) {
  switch ($op) {
    // Reindex the node when comments are added or changed
    case 'insert':
    case 'update':
    case 'delete':
    case 'publish':
    case 'unpublish':
      search_touch_node(is_array($a1) ? $a1['nid'] : $a1->nid);
      break;
  }
}
jeffschuler’s picture

I can't find cache_clear_all_like() (except in some old D4.6 stuff around the web.) Assuming it shouldn't be used in the example.

This issue affects D7 slightly differently than 5 & 6, since hook_comment() is split into individual functions for its various ops. Would/Should I be creating three separate patches for Drupal 5, 6, and 7 in situations where all are affected?

cache_clear_all_like() is still mentioned in D7's example functions for hook_comment_insert() and hook_comment_update().

How about using search_comment_insert() & search_comment_update() as examples: ?

function hook_comment_insert($form_values) {
  // Reindex the node when comments are added.
  search_touch_node($form_values['nid']);
}
function hook_comment_update($form_values) {
  // Reindex the node when comments are updated.
  search_touch_node($form_values['nid']);
}

Or just continuing with the same pattern as a few of the other hook_comment_* functions, like hook_comment_publish: ?

function hook_comment_publish($form_values) {
  drupal_set_message(t('Comment: @subject has been published', array('@subject' => $form_values['subject'])));
}
jeffschuler’s picture

Status: Active » Needs review
StatusFileSize
new1 KB

Patch for D7 HEAD using search examples mentioned in my last comment.

moshe weitzman’s picture

Status: Needs review » Reviewed & tested by the community
dries’s picture

Status: Reviewed & tested by the community » Fixed

Committed to CVS HEAD. Thanks!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

charlie-s’s picture

Why was this never committed to Drupal 6 if this was opened in 2008?

ro-no-lo’s picture

Funny Dries wrote that it was commited, but the docs are still the same.