Tutorial: Using Rules to Integrate Heartbeat and Statuses in Drupal 7

Bookmark and Share

Previously, I wrote a tutorial on integrating Heartbeat and Statuses without Rules via a custom module. Since then, the Token integration in the Rules module is now fully-functional. The following is a tutorial for integrating Heartbeat and Statuses via Rules.

Before starting, make sure you have the following modules enabled.

  • Rules
  • Rules UI
  • Heartbeat activity
  • Heartbeat activity defaults
  • Heartbeat UI
  • Rules for Heartbeat activity
  • Statuses
  • Statuses Rules

Once the modules are enabled, follow these steps.

  1. Navigate to Admin > Structure > Heartbeat > Heartbeat templates.
    (admin/structure/heartbeat/templates)
  2. Click + Add.
  3. To create a template for users posting a status to their own stream, use the settings below.
    • Message ID: heartbeat_add_status
    • Definition tab: 
      Description of the message: users posts a status message on their own stream
    • Access tab: Setup access to suit your needs. I use "Everyone can see this message".
    • Examples tab: Nothing to do. This shows you examples for variables used on the content tab.
    • Content tab:
      Single message: !username !message
      (Customize the message and mark-up if you'd like, but make sure you include the !username and !message. You could also use something like: <span>!username updated status</span><blockquote>!message</blockquote>)
      Type of message: You can choose Single or Summary here.
      Choose single if you want all statuses to appear by themselves. Choose summary if you want statuses posted by the same user within a certain timeframe grouped together.
    • Click Save.

    Create the template for single-user status
    To show every status separately, under Type of Message, choose Single: Treat all activity instances as standalone messages.
    To show all status messages, separately, choose single.

    To group messages by a single user, select Summary:Merge recurrent activity together for Type of Message and enter the settings below.
    Choose "summary" and use the settings here to group status messages within a certain period of time by a single user.

  4. You'll be taken back to the Heartbeat templates page after saving the template. Next to the heartbeat_add_status template you just created, click edit.
  5. To update the display for the heartbeat_add_status template:
    • Click the Manage Display tab near the top of the page.
    • On the Manage Display page, click locate the vertical tabs at the bottom of the page. Click the tab Layout for heartbeat add status in default.
      (Note, if you used a different Message ID, your tab text will be slightly different.)
    • For Select a layout, choose Template with left/right for activity from the drop-down.
    • Click Save.
    • When the page reloads, move the fields into the regions as follows.
      • Left:
        • Avatar
      • Content:
        • Message
      • Footer:
        • Time
        • Buttons
    • Click Save.

    Display settings for the heartbeat_add_status template

  6. On the Structure > Heartbeat > Heartbeat templates page, click + Add once again.
  7. To create a template for status messages posted by one user to another, use the settings below.
    • Message ID: heartbeat_add_status_recipient
    • Definition tab:
      Description of the message: user posts a status message to another user stream
    • Access tab: Setup access to suit your needs. I use "Everyone can see this message".
    • Content tab:
      Single message:  !username &raquo; !recipient !message
      (Again, customize the message if you'd like, but make sure to include !username, !recipient and !message. You can enter markup, such as: <span>!username posted to !recipient's profile</span><blockquote>!message</blockquote>)
       
      Type of message: Single or Summary
    • Click Save.

    Create the template for status message between users
    Enter the content and select the type of message

  8. You'll be taken back to the Heartbeat templates page after saving the template. Next to the heartbeat_add_status_recipient template you just created, click edit.
  9. To update the display for the heartbeat_add_status_recipient template.
    • Click the Manage Display tab near the top of the page.
    • On the Manage Display page, click locate the vertical tabs at the bottom of the page. Click the tab Layout for heartbeat add status in default.
      (Note, if you used a different Message ID, your tab text will be slightly different.)
    • For Select a layout, choose Template with left/right for activity from the drop-down.
    • Click Save.
    • When the page reloads, move the fields into the regions as follows.
      • Left:
        • Avatar
      • Content:
        • Message
      • Footer:
        • Time
        • Buttons
    • Click Save.

    Display settings for the heartbeat_add_status_recipient template

  10. Clear the site's cache. When I don't do this after creating the templates, I sometimes have issues trying to configure the rules.
  11. Navigate to Configuration > Workflow > Rules
  12. Click + Add new rule.
  13. To create a rule for a user posting a status to their own stream, use the following:
    Settings:
    • Name: Log activity for user posts status
    • React on event: User saves a status

    Rule settings for user posts status to own stream

    Conditions:

    • Click + Add Condition.
    • Select the condition to add: Status was posted to own profile
    • Data selector: status

    Actions:

    • Click + Add action.
    • Select the action to add: Log activity for user posts a status message on their own stream
      Select the action for the rule
    • Entity ID: 0
    • User ID: [status:sender:uid]
    • Entity Target ID: 0
    • User Target ID: [status:recipient-id]
    • Comment ID: (leave empty)
    • Message ID: user posts a status message on their own stream
    • !username: [status:sender:link]
    • !message: [status:message-formatted]
  14. Click + Add new rule again.
  15. To create a rule for a user posting a status to another user's stream, use the following:
    Settings:
    • Name: Log activity for user posts status to other user
    • React on event: User saves a status

    Rules settings for user posts status to other user

    Conditions:

    • Click + Add Condition.
    • Select the condition to add: Status was posted to own profile
    • Data selector: status
    • Check Negate
      Check negate. Add condition for user not posting status to own profile

    Actions:

    • Click + Add action.
    • Select the action to add: Log activity for user posts a status message on their own stream
      Add action for user posts status to other user
    • Entity ID: 0
    • User ID: [status:sender:uid]
    • Entity Target ID: 0
    • User Target ID: [status:recipient-id]
    • Comment ID: (leave empty)
    • Message ID: user posts a status message on their own stream
    • !username: [status:sender:link]
    • !recipient: [status:recipient-link]
    • !message: [status:message-formatted]

That's it. Post some statuses and check out your Heartbeat activity streams.

Example of a heartbeat activity user stream with statuses integrated

One final note

You may notice that if a user edits or deletes a status, the corresponding Heartbeat activity is not affected. As of the writing of this tutorial, it's not possible to automatically edit or delete the Heartbeat activity when a status message is editted/deleted. The Status module provides the necessary events "User deletes a status" and "User edits a status", but the Heartbeat module does not store an association between a status and its Heartbeat activity message.

The Heartbeat module supports only node association. Entity support is needed for Heartbeat to act on an edit or delete of other entities. See issue Remove node dependency on heartbeat.

Comments

Hello,

thanks a lot for this great tutorial. I find it very informative.

I have a question, though. Is there any way to make the heartbeat stream update automatically after the user posts a status, as it does by status updates block?

My best regards,
Elin.

Thanks for the clear step-by-step info! I'm curious what benefit heartbeat brings that Statuses doesn't have built in?

Thanks,
-Julian

Thank you for the thorough explanation. I'm rather new to both of these modules, under what circumstances is it useful to use this, vs. the built in views?

Thanks,
-Julian

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.