comment.api.php 6.17 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
 * This hook is invoked from $comment->save() before the comment is saved to the
19
 * 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
/**
 * 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.
 *
57
 * @param \Drupal\comment\Entity\Comment $comment
58 59
 *   The comment object.
 */
60
function hook_comment_create(\Drupal\comment\Entity\Comment $comment) {
61 62 63 64 65
  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\Entity\Comment $comment $comment
83
 *   Passes in the comment the action is being performed on.
84
 * @param \Drupal\entity\Entity\EntityDisplay $display
85 86
 *   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
function hook_comment_view(\Drupal\comment\Entity\Comment $comment, \Drupal\entity\Entity\EntityDisplay $display, $view_mode, $langcode) {
95 96 97 98 99 100 101 102 103
  // 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
 * callback. Alternatively, it could also implement hook_preprocess_HOOK() for
116
 * comment.html.twig. See drupal_render() documentation for details.
117 118 119
 *
 * @param $build
 *   A renderable array representing the comment.
120
 * @param \Drupal\comment\Entity\Comment $comment
121
 *   The comment being rendered.
122
 * @param \Drupal\entity\Entity\EntityDisplay $display
123 124
 *   The entity_display object holding the display options configured for the
 *   comment components.
125
 *
126
 * @see comment_view()
127
 * @see hook_entity_view_alter()
128
 */
129
function hook_comment_view_alter(&$build, \Drupal\comment\Entity\Comment $comment, \Drupal\entity\Entity\EntityDisplay $display) {
130
  // Check for the existence of a field added by another module.
131
  if ($build['#view_mode'] == 'full' && isset($build['an_additional_field'])) {
132 133 134 135 136 137 138 139
    // 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';
}

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

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

/**
161 162
 * Act before comment deletion.
 *
163
 * This hook is invoked from entity_delete_multiple() before
164 165
 * field_attach_delete() is called and before the comment is actually removed
 * from the database.
166
 *
167
 * @param Drupal\comment\Comment $comment
168 169 170 171 172
 *   The comment object for the comment that is about to be deleted.
 *
 * @see hook_comment_delete()
 * @see entity_delete_multiple()
 */
173
function hook_comment_predelete(Drupal\comment\Comment $comment) {
174 175
  // Delete a record associated with the comment in a custom table.
  db_delete('example_comment_table')
176
    ->condition('cid', $comment->id())
177 178 179 180 181 182
    ->execute();
}

/**
 * Respond to comment deletion.
 *
183
 * This hook is invoked from entity_delete_multiple() after
184 185 186
 * field_attach_delete() has called and after the comment has been removed from
 * the database.
 *
187
 * @param Drupal\comment\Comment $comment
188 189 190 191
 *   The comment object for the comment that has been deleted.
 *
 * @see hook_comment_predelete()
 * @see entity_delete_multiple()
192
 */
193
function hook_comment_delete(Drupal\comment\Comment $comment) {
194
  drupal_set_message(t('Comment: @subject has been deleted', array('@subject' => $comment->subject->value)));
195 196
}

197 198 199
/**
 * @} End of "addtogroup hooks".
 */