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