comment.api.php 5.04 KB
Newer Older
1 2
<?php

3
use Drupal\Core\Entity\EntityInterface;
4

5 6 7 8 9 10 11 12 13 14
/**
 * @file
 * Hooks provided by the Comment module.
 */

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

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

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

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

51
/**
52
 * Act on comments being loaded from the database.
53
 *
54
 * @param array $comments
55 56
 *  An array of comment objects indexed by cid.
 */
57
function hook_comment_load(Drupal\comment\Comment $comments) {
58 59 60 61 62 63
  $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;
  }
}

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

81
/**
82
 * Alter the results of comment_view().
83
 *
84 85 86
 * 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.
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
90 91 92
 * callback. Alternatively, it could also implement hook_preprocess_HOOK() for
 * comment.tpl.php. See drupal_render() and theme() documentation respectively
 * for details.
93 94 95
 *
 * @param $build
 *   A renderable array representing the comment.
96 97
 * @param Drupal\comment\Comment $comment
 *   The comment being rendered.
98
 *
99
 * @see comment_view()
100
 * @see hook_entity_view_alter()
101
 */
102
function hook_comment_view_alter(&$build, Drupal\comment\Comment $comment) {
103
  // Check for the existence of a field added by another module.
104
  if ($build['#view_mode'] == 'full' && isset($build['an_additional_field'])) {
105 106 107 108 109 110 111 112
    // 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';
}

113
/**
114
 * Respond to a comment being published by a moderator.
115
 *
116
 * @param Drupal\comment\Comment $comment
117
 *   The comment the action is being performed on.
118
 */
119
function hook_comment_publish(Drupal\comment\Comment $comment) {
120
  drupal_set_message(t('Comment: @subject has been published', array('@subject' => $comment->subject)));
121 122 123
}

/**
124
 * Respond to a comment being unpublished by a moderator.
125
 *
126
 * @param Drupal\comment\Comment $comment
127
 *   The comment the action is being performed on.
128
 */
129
function hook_comment_unpublish(Drupal\comment\Comment $comment) {
130 131 132 133
  drupal_set_message(t('Comment: @subject has been unpublished', array('@subject' => $comment->subject)));
}

/**
134 135 136 137 138
 * 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.
139
 *
140
 * @param Drupal\comment\Comment $comment
141 142 143 144 145 146
 *   The comment object for the comment that is about to be deleted.
 *
 * @see hook_comment_delete()
 * @see comment_delete_multiple()
 * @see entity_delete_multiple()
 */
147
function hook_comment_predelete(Drupal\comment\Comment $comment) {
148 149 150 151 152 153 154 155 156 157 158 159 160
  // 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.
 *
161
 * @param Drupal\comment\Comment $comment
162 163 164 165 166
 *   The comment object for the comment that has been deleted.
 *
 * @see hook_comment_predelete()
 * @see comment_delete_multiple()
 * @see entity_delete_multiple()
167
 */
168
function hook_comment_delete(Drupal\comment\Comment $comment) {
169 170 171
  drupal_set_message(t('Comment: @subject has been deleted', array('@subject' => $comment->subject)));
}

172 173 174
/**
 * @} End of "addtogroup hooks".
 */