Tutorial: Integrate Statuses with Heartbeat in Drupal 7

Bookmark and Share
UPDATE: Token support is now functional for Statuses, making integration with Heartbeat possible via Rules. Please see Using Rules to Integrate Heartbeat and Statuses in Drupal 7.

The Drupal Statuses module provides the ability for users to post status messages to their profiles and the profiles of other users. The Heartbeat module provides the ability to show a list of all user activity on a site. As of the writing of this post, you were not able to integrate the Drupal Statuses module with the Heartbeat module. The Rules module should be able to tie the two together, but due to existing issues with Statuses token integration, this is not currently possible.
If you find yourself unable to get Statuses to integrate with Heartbeat via Rules, you can use this method to integrate the two. The Rules module is not needed for this method.
  1. Navigate to Admin > Structure > Heartbeat > Heartbeat templates. (admin/structure/heartbeat/templates)
  2. Click "+Add".  (You should be on the page admin/structure/heartbeat/templates/add)
  3. Create the new template using the settings below. After these are set, click "Save".
    • Message ID: heartbeat_add_status
    • Definition tab: 
      Description of the message: user posted status update
    • Access tab: Setup access to suit your needs. I use "Everyone can see this message".
    • Examples tab: Nothing to do
    • Content tab:
      Single message: <span>!username updated status</span><blockquote><div>!message</div></blockquote>
      (Customize the message if you'd like, but make sure you include the !username and !message)
      Type of message: Single: Treat all activity instances as standalone messages 
    • Attachments tab: Turn on any attachments you want to use. (Note: I do not use any, so I haven't tested them with this method.)

     

  4. On the heartbeat templates page (admin/structure/heartbeat/templates), click "+Add" once again.
  5. Create another template using the settings below. After these are set, click "Save".
    • Message ID: heartbeat_add_status_recipient
    • Definition tab:
      Description of the message: user posts a status message to another user's profile 
    • Access tab: Setup access to suit your needs. I use "Everyone can see this message".
    • Examples tab: Nothing to do here.
    • Content tab:
      Single message:  <span>!username posted to !recipient's profile</span><blockquote><div>!message</div></blockquote>
      (Customize the message if you'd like, but make sure to include !username, !recipient and !message.)
       
      Type of message: Single: Treat all activity instances as standalone messages
    •  Attachments tab: Turn on any attachments you want to use. (Note: I do not use any, so I haven't tested them with this method.)

     

  6. The next step is the hardest part, but not too hard if you know how to create a module for Drupal. If you don't, find instructions at http://drupal.org/node/416986. In short, you only need to create files [MODULE].info, [MODULE].module, and optionally, [MODULE].install.  ([MODULE] is a placeholder for whatever you want to call your module.)

    Inside your .module file, use the code below. Make sure you got the names of the templates in the steps above correct, because the code here references those templates. 

 

/*
 * Implements hook_statuses_save
 * Integration with rules for statuses doesn't work on the current release
 * as of 4/9/12. so this hook updates the heartbeat with the statuses updates
 * See https://drupal.org/node/1255648
 */
function [MODULE]_statuses_save($status, $context, $edit, $options) {
  if ($edit) {
    //If you want activity logged for status edit, you could do it here
  }
  else {
    $variables = array(
      '!message' => $status->message,
      '!username' => l(user_load($status->sender)->name, 'user/'. $status->sender),
    );
    $recipient = $status->sender == $status->recipient ? 0 : $status->recipient;
    $entity_id = 0; //If Heartbeat supported entities, would be $status->sid;
    if($recipient) {
       $variables['!recipient'] = l(user_load($status->recipient)->name, 
        'user/' . $status->recipient);
       heartbeat_api_log('heartbeat_add_status_recipient', $status->sender, 
        $recipient, $entity_id, 0, $variables, HEARTBEAT_PUBLIC_TO_ALL, time());
    }
    else {
      heartbeat_api_log('heartbeat_add_status', $status->sender, $recipient, 
       $entity_id, 0, $variables, HEARTBEAT_PUBLIC_TO_ALL, time());
    }
  }
}

Comments

This is exactly what I was needed, thank you! I was able to update the heartbeat stream with the statuses!

I was wondering if it is possible to integrate with Facebook-style Micropublisher? (http://drupal.org/project/fbsmp)
Using the above, the stream will only show the status update, but nothing will be shown for content via fbsmp (as it does for example in core statuses module).
Any ideas how to include links etc from fbsmp in heartbeat in D7?

Any help/pointers would be greatly appreciated!
Thanks!

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.