comment.api.php 7.7 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->setSubject(trim($comment->getSubject()));
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
  if ($comment->getCommentedEntityTypeId() == 'node') {
    node_reindex_node_search($comment->getCommentedEntityId());
40
  }
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
  if ($comment->getCommentedEntityTypeId() == 'node') {
    node_reindex_node_search($comment->getCommentedEntityId());
53
  }
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 88
 * @param array &$build
 *   A renderable array representing the comment content.
89
 * @param \Drupal\comment\Entity\Comment $comment $comment
90
 *   Passes in the comment the action is being performed on.
91
 * @param \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display
92
 *   The entity view display holding the display options configured for the
93
 *   comment components.
94 95 96 97
 * @param $view_mode
 *   View mode, e.g. 'full', 'teaser'...
 * @param $langcode
 *   The language code used for rendering.
98 99
 *
 * @see hook_entity_view()
100
 */
101
function hook_comment_view(array &$build, \Drupal\comment\Entity\Comment $comment, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode, $langcode) {
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
104
  // node type in hook_entity_extra_field_info().
105
  if ($display->getComponent('mymodule_addition')) {
106
    $build['mymodule_addition'] = array(
107 108 109 110
      '#markup' => mymodule_addition($comment),
      '#theme' => 'mymodule_my_additional_field',
    );
  }
111 112
}

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

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

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

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

/**
 * Respond to comment deletion.
 *
189 190
 * This hook is invoked from entity_delete_multiple() after field values are
 * deleted and after the comment has been removed from the database.
191
 *
192
 * @param \Drupal\comment\Comment $comment
193 194 195 196
 *   The comment object for the comment that has been deleted.
 *
 * @see hook_comment_predelete()
 * @see entity_delete_multiple()
197
 */
198
function hook_comment_delete(Drupal\comment\Comment $comment) {
199
  drupal_set_message(t('Comment: @subject has been deleted', array('@subject' => $comment->getSubject())));
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 232 233
/**
 * 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")),
      ),
    ),
  );
}

234 235 236
/**
 * @} End of "addtogroup hooks".
 */