You are here

function commons_activity_streams_comment_insert in Drupal Commons 7.3

Implements hook_comment_insert().

File

modules/commons/commons_activity_streams/commons_activity_streams.module, line 54

Code

function commons_activity_streams_comment_insert($comment) {
  $account = user_load($comment->uid);
  $node = node_load($comment->nid);

  // Allow other modules to change the message type used for this event.
  $hook = 'comment_insert';
  $message_type = 'commons_activity_streams_comment_created';
  drupal_alter('commons_activity_streams_message_selection', $message_type, $hook, $node);
  $message = message_create($message_type, array(
    'uid' => $account->uid,
    'timestamp' => $comment->created,
  ));

  // Save reference to the node in the node reference field, and the
  // "publish" state (i.e. if the node is published or unpublished).
  $wrapper = entity_metadata_wrapper('message', $message);
  $wrapper->field_target_nodes[] = $node;
  $wrapper->field_target_comments[] = $comment;

  // The message should be published only if the node and the comment are
  // both published.
  // @todo: Deal with message publishing/unpublishing.

  /*
  $published = $node->status && $comment->status;
  $wrapper->field_published->set($published);
  */
  $wrapper
    ->save();
}