comment.api.php 5.94 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
  // Remove leading & trailing spaces from the comment subject.
26
  $comment->subject->value = trim($comment->subject->value);
27 28
}

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->value);
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->value);
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\Plugin\Core\Entity\Comment $comment $comment
68
 *   Passes in the comment the action is being performed on.
69 70 71
 * @param \Drupal\entity\Plugin\Core\Entity\EntityDisplay $display
 *   The entity_display object holding the display options configured for the
 *   comment components.
72 73 74 75
 * @param $view_mode
 *   View mode, e.g. 'full', 'teaser'...
 * @param $langcode
 *   The language code used for rendering.
76 77
 *
 * @see hook_entity_view()
78
 */
79 80 81 82 83 84 85 86 87 88
function hook_comment_view(\Drupal\comment\Plugin\Core\Entity\Comment $comment, \Drupal\entity\Plugin\Core\Entity\EntityDisplay $display, $view_mode, $langcode) {
  // Only do the extra work if the component is configured to be displayed.
  // This assumes a 'mymodule_addition' extra field has been defined for the
  // node type in hook_field_extra_fields().
  if ($display->getComponent('mymodule_addition')) {
    $comment->content['mymodule_addition'] = array(
      '#markup' => mymodule_addition($comment),
      '#theme' => 'mymodule_my_additional_field',
    );
  }
89 90
}

91
/**
92
 * Alter the results of comment_view().
93
 *
94 95 96
 * 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.
97
 *
98 99
 * 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
100 101 102
 * callback. Alternatively, it could also implement hook_preprocess_HOOK() for
 * comment.tpl.php. See drupal_render() and theme() documentation respectively
 * for details.
103 104 105
 *
 * @param $build
 *   A renderable array representing the comment.
106
 * @param \Drupal\comment\Plugin\Core\Entity\Comment $comment
107
 *   The comment being rendered.
108 109 110
 * @param \Drupal\entity\Plugin\Core\Entity\EntityDisplay $display
 *   The entity_display object holding the display options configured for the
 *   comment components.
111
 *
112
 * @see comment_view()
113
 * @see hook_entity_view_alter()
114
 */
115
function hook_comment_view_alter(&$build, \Drupal\comment\Plugin\Core\Entity\Comment $comment, \Drupal\entity\Plugin\Core\Entity\EntityDisplay $display) {
116
  // Check for the existence of a field added by another module.
117
  if ($build['#view_mode'] == 'full' && isset($build['an_additional_field'])) {
118 119 120 121 122 123 124 125
    // 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';
}

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

/**
137
 * Respond to a comment being unpublished by a moderator.
138
 *
139
 * @param Drupal\comment\Comment $comment
140
 *   The comment the action is being performed on.
141
 */
142
function hook_comment_unpublish(Drupal\comment\Comment $comment) {
143
  drupal_set_message(t('Comment: @subject has been unpublished', array('@subject' => $comment->subject->value)));
144 145 146
}

/**
147 148 149 150 151
 * 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.
152
 *
153
 * @param Drupal\comment\Comment $comment
154 155 156 157 158 159
 *   The comment object for the comment that is about to be deleted.
 *
 * @see hook_comment_delete()
 * @see comment_delete_multiple()
 * @see entity_delete_multiple()
 */
160
function hook_comment_predelete(Drupal\comment\Comment $comment) {
161 162
  // Delete a record associated with the comment in a custom table.
  db_delete('example_comment_table')
163
    ->condition('cid', $comment->id())
164 165 166 167 168 169 170 171 172 173
    ->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.
 *
174
 * @param Drupal\comment\Comment $comment
175 176 177 178 179
 *   The comment object for the comment that has been deleted.
 *
 * @see hook_comment_predelete()
 * @see comment_delete_multiple()
 * @see entity_delete_multiple()
180
 */
181
function hook_comment_delete(Drupal\comment\Comment $comment) {
182
  drupal_set_message(t('Comment: @subject has been deleted', array('@subject' => $comment->subject->value)));
183 184
}

185 186 187
/**
 * @} End of "addtogroup hooks".
 */