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

3
use Drupal\Core\Entity\EntityInterface;
4
use Drupal\comment\CommentInterface;
5

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

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

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

30
/**
31
 * Respond to creation of a new comment.
32
 *
33
 * @param \Drupal\comment\Comment $comment
34
 *   The comment object.
35
 */
36
function hook_comment_insert(Drupal\comment\Comment $comment) {
37
  // Reindex the node when comments are added.
38 39 40
  if ($comment->entity_type->value == 'node') {
    node_reindex_node_search($comment->entity_id->value);
  }
41 42 43
}

/**
44
 * Respond to updates to a comment.
45
 *
46
 * @param \Drupal\comment\Comment $comment
47
 *   The comment object.
48
 */
49
function hook_comment_update(Drupal\comment\Comment $comment) {
50
  // Reindex the node when comments are updated.
51 52 53
  if ($comment->entity_type->value == 'node') {
    node_reindex_node_search($comment->entity_id->value);
  }
54 55
}

56 57 58 59 60 61
/**
 * 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.
 *
62
 * @param \Drupal\comment\Entity\Comment $comment
63 64
 *   The comment object.
 */
65
function hook_comment_create(\Drupal\comment\Entity\Comment $comment) {
66 67 68 69 70
  if (!isset($comment->foo)) {
    $comment->foo = 'some_initial_value';
  }
}

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

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

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

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

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

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

/**
 * Respond to comment deletion.
 *
187 188
 * This hook is invoked from entity_delete_multiple() after field values are
 * deleted and after the comment has been removed from the database.
189
 *
190
 * @param \Drupal\comment\Comment $comment
191 192 193 194
 *   The comment object for the comment that has been deleted.
 *
 * @see hook_comment_predelete()
 * @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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
/**
 * Alter the links of a comment.
 *
 * @param array &$links
 *   A renderable array representing the comment links.
 * @param \Drupal\comment\CommentInterface $entity
 *   The comment being rendered.
 * @param array &$context
 *   Various aspects of the context in which the comment links are going to be
 *   displayed, with the following keys:
 *   - 'view_mode': the view mode in which the comment is being viewed
 *   - 'langcode': the language in which the comment is being viewed
 *   - 'commented_entity': the entity to which the comment is attached
 *
 * @see \Drupal\comment\CommentViewBuilder::renderLinks()
 * @see \Drupal\comment\CommentViewBuilder::buildLinks()
 */
function hook_comment_links_alter(array &$links, CommentInterface $entity, array &$context) {
  $links['mymodule'] = array(
    '#theme' => 'links__comment__mymodule',
    '#attributes' => array('class' => array('links', 'inline')),
    '#links' => array(
      'comment-report' => array(
        'title' => t('Report'),
        'href' => "comment/{$entity->id()}/report",
        'html' => TRUE,
        'query' => array('token' => \Drupal::getContainer()->get('csrf_token')->get("comment/{$entity->id()}/report")),
      ),
    ),
  );
}

232 233 234
/**
 * @} End of "addtogroup hooks".
 */