comment.api.php

Hooks provided by the Comment module.

File

drupal-7.x/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. * The comment passed validation and is about to be saved.
  12. *
  13. * Modules may make changes to the comment before it is saved to the database.
  14. *
  15. * @param $comment
  16. * The comment object.
  17. */
  18. function hook_comment_presave($comment) {
  19. // Remove leading & trailing spaces from the comment subject.
  20. $comment->subject = trim($comment->subject);
  21. }
  22. /**
  23. * The comment is being inserted.
  24. *
  25. * @param $comment
  26. * The comment object.
  27. */
  28. function hook_comment_insert($comment) {
  29. // Reindex the node when comments are added.
  30. search_touch_node($comment->nid);
  31. }
  32. /**
  33. * The comment is being updated.
  34. *
  35. * @param $comment
  36. * The comment object.
  37. */
  38. function hook_comment_update($comment) {
  39. // Reindex the node when comments are updated.
  40. search_touch_node($comment->nid);
  41. }
  42. /**
  43. * Comments are being loaded from the database.
  44. *
  45. * @param $comments
  46. * An array of comment objects indexed by cid.
  47. */
  48. function hook_comment_load($comments) {
  49. $result = db_query('SELECT cid, foo FROM {mytable} WHERE cid IN (:cids)', array(':cids' => array_keys($comments)));
  50. foreach ($result as $record) {
  51. $comments[$record->cid]->foo = $record->foo;
  52. }
  53. }
  54. /**
  55. * The comment is being viewed. This hook can be used to add additional data to the comment before theming.
  56. *
  57. * @param $comment
  58. * Passes in the comment the action is being performed on.
  59. * @param $view_mode
  60. * View mode, e.g. 'full', 'teaser'...
  61. * @param $langcode
  62. * The language code used for rendering.
  63. *
  64. * @see hook_entity_view()
  65. */
  66. function hook_comment_view($comment, $view_mode, $langcode) {
  67. // how old is the comment
  68. $comment->time_ago = time() - $comment->changed;
  69. }
  70. /**
  71. * The comment was built; the module may modify the structured content.
  72. *
  73. * This hook is called after the content has been assembled in a structured array
  74. * and may be used for doing processing which requires that the complete comment
  75. * content structure has been built.
  76. *
  77. * If the module wishes to act on the rendered HTML of the comment rather than the
  78. * structured content array, it may use this hook to add a #post_render callback.
  79. * Alternatively, it could also implement hook_preprocess_comment(). See
  80. * drupal_render() and theme() documentation respectively for details.
  81. *
  82. * @param $build
  83. * A renderable array representing the comment.
  84. *
  85. * @see comment_view()
  86. * @see hook_entity_view_alter()
  87. */
  88. function hook_comment_view_alter(&$build) {
  89. // Check for the existence of a field added by another module.
  90. if ($build['#view_mode'] == 'full' && isset($build['an_additional_field'])) {
  91. // Change its weight.
  92. $build['an_additional_field']['#weight'] = -10;
  93. }
  94. // Add a #post_render callback to act on the rendered HTML of the comment.
  95. $build['#post_render'][] = 'my_module_comment_post_render';
  96. }
  97. /**
  98. * The comment is being published by the moderator.
  99. *
  100. * @param $comment
  101. * Passes in the comment the action is being performed on.
  102. * @return
  103. * Nothing.
  104. */
  105. function hook_comment_publish($comment) {
  106. drupal_set_message(t('Comment: @subject has been published', array('@subject' => $comment->subject)));
  107. }
  108. /**
  109. * The comment is being unpublished by the moderator.
  110. *
  111. * @param $comment
  112. * Passes in the comment the action is being performed on.
  113. * @return
  114. * Nothing.
  115. */
  116. function hook_comment_unpublish($comment) {
  117. drupal_set_message(t('Comment: @subject has been unpublished', array('@subject' => $comment->subject)));
  118. }
  119. /**
  120. * The comment is being deleted by the moderator.
  121. *
  122. * @param $comment
  123. * Passes in the comment the action is being performed on.
  124. * @return
  125. * Nothing.
  126. */
  127. function hook_comment_delete($comment) {
  128. drupal_set_message(t('Comment: @subject has been deleted', array('@subject' => $comment->subject)));
  129. }
  130. /**
  131. * @} End of "addtogroup hooks".
  132. */