comment.api.php 4.49 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
<?php

/**
 * @file
 * Hooks provided by the Comment module.
 */

/**
 * @addtogroup hooks
 * @{
 */

13
/**
14
 * Act on a comment being inserted or updated.
15
 *
16 17
 * This hook is invoked from comment_save() before the comment is saved to the
 * database.
18 19 20 21 22 23 24 25 26
 *
 * @param $comment
 *   The comment object.
 */
function hook_comment_presave($comment) {
  // Remove leading & trailing spaces from the comment subject.
  $comment->subject = trim($comment->subject);
}

27
/**
28
 * Respond to creation of a new comment.
29
 *
30 31
 * @param $comment
 *   The comment object.
32
 */
33
function hook_comment_insert($comment) {
34
  // Reindex the node when comments are added.
35
  search_touch_node($comment->nid);
36 37 38
}

/**
39
 * Respond to updates to a comment.
40
 *
41 42
 * @param $comment
 *   The comment object.
43
 */
44
function hook_comment_update($comment) {
45
  // Reindex the node when comments are updated.
46
  search_touch_node($comment->nid);
47 48
}

49
/**
50
 * Act on comments being loaded from the database.
51 52 53 54 55 56 57 58 59 60 61
 *
 * @param $comments
 *  An array of comment objects indexed by cid.
 */
function hook_comment_load($comments) {
  $result = db_query('SELECT cid, foo FROM {mytable} WHERE cid IN (:cids)', array(':cids' => array_keys($comments)));
  foreach ($result as $record) {
    $comments[$record->cid]->foo = $record->foo;
  }
}

62
/**
63
 * Act on a comment that is being assembled before rendering.
64 65 66
 *
 * @param $comment
 *   Passes in the comment the action is being performed on.
67 68 69 70
 * @param $view_mode
 *   View mode, e.g. 'full', 'teaser'...
 * @param $langcode
 *   The language code used for rendering.
71 72
 *
 * @see hook_entity_view()
73
 */
74
function hook_comment_view($comment, $view_mode, $langcode) {
75
  // how old is the comment
76
  $comment->time_ago = time() - $comment->changed;
77 78
}

79
/**
80
 * Alter the results of comment_view().
81
 *
82 83 84
 * This hook is called after the content has been assembled in a structured
 * array and may be used for doing processing which requires that the complete
 * comment content structure has been built.
85
 *
86 87 88 89
 * If the module wishes to act on the rendered HTML of the comment rather than
 * the structured content array, it may use this hook to add a #post_render
 * callback. Alternatively, it could also implement hook_preprocess_comment().
 * See drupal_render() and theme() documentation respectively for details.
90 91 92 93
 *
 * @param $build
 *   A renderable array representing the comment.
 *
94
 * @see comment_view()
95
 * @see hook_entity_view_alter()
96
 */
97
function hook_comment_view_alter(&$build) {
98
  // Check for the existence of a field added by another module.
99
  if ($build['#view_mode'] == 'full' && isset($build['an_additional_field'])) {
100 101 102 103 104 105 106 107
    // Change its weight.
    $build['an_additional_field']['#weight'] = -10;
  }

  // Add a #post_render callback to act on the rendered HTML of the comment.
  $build['#post_render'][] = 'my_module_comment_post_render';
}

108
/**
109
 * Respond to a comment being published by a moderator.
110
 *
111
 * @param $comment
112
 *   The comment the action is being performed on.
113
 */
114 115
function hook_comment_publish($comment) {
  drupal_set_message(t('Comment: @subject has been published', array('@subject' => $comment->subject)));
116 117 118
}

/**
119
 * Respond to a comment being unpublished by a moderator.
120 121
 *
 * @param $comment
122
 *   The comment the action is being performed on.
123
 */
124
function hook_comment_unpublish($comment) {
125 126 127 128
  drupal_set_message(t('Comment: @subject has been unpublished', array('@subject' => $comment->subject)));
}

/**
129 130 131 132 133
 * Act before comment deletion.
 *
 * This hook is invoked from comment_delete_multiple() before
 * field_attach_delete() is called and before the comment is actually removed
 * from the database.
134 135
 *
 * @param $comment
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
 *   The comment object for the comment that is about to be deleted.
 *
 * @see hook_comment_delete()
 * @see comment_delete_multiple()
 * @see entity_delete_multiple()
 */
function hook_comment_predelete($comment) {
  // Delete a record associated with the comment in a custom table.
  db_delete('example_comment_table')
    ->condition('cid', $comment->cid)
    ->execute();
}

/**
 * Respond to comment deletion.
 *
 * This hook is invoked from comment_delete_multiple() after
 * field_attach_delete() has called and after the comment has been removed from
 * the database.
 *
 * @param $comment
 *   The comment object for the comment that has been deleted.
 *
 * @see hook_comment_predelete()
 * @see comment_delete_multiple()
 * @see entity_delete_multiple()
162
 */
163
function hook_comment_delete($comment) {
164 165 166
  drupal_set_message(t('Comment: @subject has been deleted', array('@subject' => $comment->subject)));
}

167 168 169
/**
 * @} End of "addtogroup hooks".
 */