comment.api.php 6.38 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->target_id);
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->target_id);
49 50
}

51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
/**
 * Act on a newly created comment.
 *
 * This hook runs after a new comment object has just been instantiated. It can
 * be used to set initial values, e.g. to provide defaults.
 *
 * @param \Drupal\comment\Plugin\Core\Entity\Comment $comment
 *   The comment object.
 */
function hook_comment_create(\Drupal\comment\Plugin\Core\Entity\Comment $comment) {
  if (!isset($comment->foo)) {
    $comment->foo = 'some_initial_value';
  }
}

66
/**
67
 * Act on comments being loaded from the database.
68
 *
69
 * @param array $comments
70 71
 *  An array of comment objects indexed by cid.
 */
72
function hook_comment_load(Drupal\comment\Comment $comments) {
73 74 75 76 77 78
  $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;
  }
}

79
/**
80
 * Act on a comment that is being assembled before rendering.
81
 *
82
 * @param \Drupal\comment\Plugin\Core\Entity\Comment $comment $comment
83
 *   Passes in the comment the action is being performed on.
84 85 86
 * @param \Drupal\entity\Plugin\Core\Entity\EntityDisplay $display
 *   The entity_display object holding the display options configured for the
 *   comment components.
87 88 89 90
 * @param $view_mode
 *   View mode, e.g. 'full', 'teaser'...
 * @param $langcode
 *   The language code used for rendering.
91 92
 *
 * @see hook_entity_view()
93
 */
94 95 96 97 98 99 100 101 102 103
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',
    );
  }
104 105
}

106
/**
107
 * Alter the results of comment_view().
108
 *
109 110 111
 * 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.
112
 *
113 114
 * 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
115 116 117
 * callback. Alternatively, it could also implement hook_preprocess_HOOK() for
 * comment.tpl.php. See drupal_render() and theme() documentation respectively
 * for details.
118 119 120
 *
 * @param $build
 *   A renderable array representing the comment.
121
 * @param \Drupal\comment\Plugin\Core\Entity\Comment $comment
122
 *   The comment being rendered.
123 124 125
 * @param \Drupal\entity\Plugin\Core\Entity\EntityDisplay $display
 *   The entity_display object holding the display options configured for the
 *   comment components.
126
 *
127
 * @see comment_view()
128
 * @see hook_entity_view_alter()
129
 */
130
function hook_comment_view_alter(&$build, \Drupal\comment\Plugin\Core\Entity\Comment $comment, \Drupal\entity\Plugin\Core\Entity\EntityDisplay $display) {
131
  // Check for the existence of a field added by another module.
132
  if ($build['#view_mode'] == 'full' && isset($build['an_additional_field'])) {
133 134 135 136 137 138 139 140
    // 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';
}

141
/**
142
 * Respond to a comment being published by a moderator.
143
 *
144
 * @param Drupal\comment\Comment $comment
145
 *   The comment the action is being performed on.
146
 */
147
function hook_comment_publish(Drupal\comment\Comment $comment) {
148
  drupal_set_message(t('Comment: @subject has been published', array('@subject' => $comment->subject->value)));
149 150 151
}

/**
152
 * Respond to a comment being unpublished by a moderator.
153
 *
154
 * @param Drupal\comment\Comment $comment
155
 *   The comment the action is being performed on.
156
 */
157
function hook_comment_unpublish(Drupal\comment\Comment $comment) {
158
  drupal_set_message(t('Comment: @subject has been unpublished', array('@subject' => $comment->subject->value)));
159 160 161
}

/**
162 163 164 165 166
 * 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.
167
 *
168
 * @param Drupal\comment\Comment $comment
169 170 171 172 173 174
 *   The comment object for the comment that is about to be deleted.
 *
 * @see hook_comment_delete()
 * @see comment_delete_multiple()
 * @see entity_delete_multiple()
 */
175
function hook_comment_predelete(Drupal\comment\Comment $comment) {
176 177
  // Delete a record associated with the comment in a custom table.
  db_delete('example_comment_table')
178
    ->condition('cid', $comment->id())
179 180 181 182 183 184 185 186 187 188
    ->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.
 *
189
 * @param Drupal\comment\Comment $comment
190 191 192 193 194
 *   The comment object for the comment that has been deleted.
 *
 * @see hook_comment_predelete()
 * @see comment_delete_multiple()
 * @see entity_delete_multiple()
195
 */
196
function hook_comment_delete(Drupal\comment\Comment $comment) {
197
  drupal_set_message(t('Comment: @subject has been deleted', array('@subject' => $comment->subject->value)));
198 199
}

200 201 202
/**
 * @} End of "addtogroup hooks".
 */