comment.api.php

  1. drupal
    1. 7 modules/comment/comment.api.php
    2. 8 core/modules/comment/comment.api.php

Hooks provided by the Comment module.

Functions & methods

NameDescription
hook_comment_deleteRespond to comment deletion.
hook_comment_insertRespond to creation of a new comment.
hook_comment_loadAct on comments being loaded from the database.
hook_comment_predeleteAct before comment deletion.
hook_comment_presaveAct on a comment being inserted or updated.
hook_comment_publishRespond to a comment being published by a moderator.
hook_comment_unpublishRespond to a comment being unpublished by a moderator.
hook_comment_updateRespond to updates to a comment.
hook_comment_viewAct on a comment that is being assembled before rendering.
hook_comment_view_alterAlter the results of comment_view().

File

core/modules/comment/comment.api.php
View source
  1. <?php
  2. /**
  3. * @file
  4. * Hooks provided by the Comment module.
  5. */
  6. /**
  7. * @addtogroup hooks
  8. * @{
  9. */
  10. /**
  11. * Act on a comment being inserted or updated.
  12. *
  13. * This hook is invoked from comment_save() before the comment is saved to the
  14. * database.
  15. *
  16. * @param Drupal\comment\Comment $comment
  17. * The comment object.
  18. */
  19. function hook_comment_presave(DrupalcommentComment $comment) {
  20. // Remove leading & trailing spaces from the comment subject.
  21. $comment->subject = trim($comment->subject);
  22. }
  23. /**
  24. * Respond to creation of a new comment.
  25. *
  26. * @param Drupal\comment\Comment $comment
  27. * The comment object.
  28. */
  29. function hook_comment_insert(DrupalcommentComment $comment) {
  30. // Reindex the node when comments are added.
  31. search_touch_node($comment->nid);
  32. }
  33. /**
  34. * Respond to updates to a comment.
  35. *
  36. * @param Drupal\comment\Comment $comment
  37. * The comment object.
  38. */
  39. function hook_comment_update(DrupalcommentComment $comment) {
  40. // Reindex the node when comments are updated.
  41. search_touch_node($comment->nid);
  42. }
  43. /**
  44. * Act on comments being loaded from the database.
  45. *
  46. * @param array $comments
  47. * An array of comment objects indexed by cid.
  48. */
  49. function hook_comment_load(DrupalcommentComment $comments) {
  50. $result = db_query('SELECT cid, foo FROM {mytable} WHERE cid IN (:cids)', array(':cids' => array_keys($comments)));
  51. foreach ($result as $record) {
  52. $comments[$record->cid]->foo = $record->foo;
  53. }
  54. }
  55. /**
  56. * Act on a comment that is being assembled before rendering.
  57. *
  58. * @param Drupal\comment\Comment $comment
  59. * Passes in the comment the action is being performed on.
  60. * @param $view_mode
  61. * View mode, e.g. 'full', 'teaser'...
  62. * @param $langcode
  63. * The language code used for rendering.
  64. *
  65. * @see hook_entity_view()
  66. */
  67. function hook_comment_view(DrupalcommentComment $comment, $view_mode, $langcode) {
  68. // how old is the comment
  69. $comment->time_ago = time() - $comment->changed;
  70. }
  71. /**
  72. * Alter the results of comment_view().
  73. *
  74. * This hook is called after the content has been assembled in a structured
  75. * array and may be used for doing processing which requires that the complete
  76. * comment content structure has been built.
  77. *
  78. * If the module wishes to act on the rendered HTML of the comment rather than
  79. * the structured content array, it may use this hook to add a #post_render
  80. * callback. Alternatively, it could also implement hook_preprocess_HOOK() for
  81. * comment.tpl.php. See drupal_render() and theme() documentation respectively
  82. * for details.
  83. *
  84. * @param $build
  85. * A renderable array representing the comment.
  86. *
  87. * @see comment_view()
  88. * @see hook_entity_view_alter()
  89. */
  90. function hook_comment_view_alter(&$build) {
  91. // Check for the existence of a field added by another module.
  92. if ($build['#view_mode'] == 'full' && isset($build['an_additional_field'])) {
  93. // Change its weight.
  94. $build['an_additional_field']['#weight'] = -10;
  95. }
  96. // Add a #post_render callback to act on the rendered HTML of the comment.
  97. $build['#post_render'][] = 'my_module_comment_post_render';
  98. }
  99. /**
  100. * Respond to a comment being published by a moderator.
  101. *
  102. * @param Drupal\comment\Comment $comment
  103. * The comment the action is being performed on.
  104. */
  105. function hook_comment_publish(DrupalcommentComment $comment) {
  106. drupal_set_message(t('Comment: @subject has been published', array('@subject' => $comment->subject)));
  107. }
  108. /**
  109. * Respond to a comment being unpublished by a moderator.
  110. *
  111. * @param Drupal\comment\Comment $comment
  112. * The comment the action is being performed on.
  113. */
  114. function hook_comment_unpublish(DrupalcommentComment $comment) {
  115. drupal_set_message(t('Comment: @subject has been unpublished', array('@subject' => $comment->subject)));
  116. }
  117. /**
  118. * Act before comment deletion.
  119. *
  120. * This hook is invoked from comment_delete_multiple() before
  121. * field_attach_delete() is called and before the comment is actually removed
  122. * from the database.
  123. *
  124. * @param Drupal\comment\Comment $comment
  125. * The comment object for the comment that is about to be deleted.
  126. *
  127. * @see hook_comment_delete()
  128. * @see comment_delete_multiple()
  129. * @see entity_delete_multiple()
  130. */
  131. function hook_comment_predelete(DrupalcommentComment $comment) {
  132. // Delete a record associated with the comment in a custom table.
  133. db_delete('example_comment_table')
  134. ->condition('cid', $comment->cid)
  135. ->execute();
  136. }
  137. /**
  138. * Respond to comment deletion.
  139. *
  140. * This hook is invoked from comment_delete_multiple() after
  141. * field_attach_delete() has called and after the comment has been removed from
  142. * the database.
  143. *
  144. * @param Drupal\comment\Comment $comment
  145. * The comment object for the comment that has been deleted.
  146. *
  147. * @see hook_comment_predelete()
  148. * @see comment_delete_multiple()
  149. * @see entity_delete_multiple()
  150. */
  151. function hook_comment_delete(DrupalcommentComment $comment) {
  152. drupal_set_message(t('Comment: @subject has been deleted', array('@subject' => $comment->subject)));
  153. }
  154. /**
  155. * @} End of "addtogroup hooks".
  156. */
Login or register to post comments