This is meant to be a reasonable tutorial for using message_subscribe with organic groups as the module exists as of (2014-02-25), but you should be able to extrapolate to make this work in other contexts. Since I found this module relatively complicated to set up and configure, I hope this helps someone have an easier time of it. As the module develops further, this documentation should be updated.

Currently (as of 2014-02-25) to get message_subscribe fully functional with og, you should use a patched version of the most recent git commit of the message_subscribe module. Without the most recent commit (commit 244a8879f722083d799a8c0522c368a44c5cda68), a single subscriber who is also the author of the group cannot receive messages. Hopefully the patches I recommend will get merged into a production release soon.

Issues:

To start, clone the git repository of the module and apply the following patches:

Download/Enable the following modules:

  • og
  • og_ui
  • message_subscribe
  • message_subscribe_ui
  • message_subscribe_email
  • views_ui

Message subscribe should enable multiple modules in addition to it's own as it depends on ctools, flag, message, and message_notify, which also depend on other modules (views).

Shell commands if you're using drush and a flavor of Linux/UNIX

git clone http://git.drupal.org/project/message_subscribe 
cd sites/all/modules/message_subscribe 
wget https://drupal.org/files/issues/email-notification-context-fix-2126689-20.patch && patch -p1 < email-notification-context-fix-2126689-20.patch 
wget https://drupal.org/files/issues/message_subscribe-subscribe_og_fix-1983226-7.patch && patch -p1 < message_subscribe-subscribe_og_fix-1983226-7.patch 
wget https://drupal.org/files/issues/message-rendered-fields-2205243-0.patch && patch -p1 < message-rendered-fields-2205243-0.patch 
drush -y en og og_ui message_subscribe message_subscribe_email message_subscribe_ui views_ui entity_token

Create an organic group type from "/admin/structure/types/add".

Once you have these modules installed, enable two flags from "/admin/structure/flags":

subscribe_og

-- under Flag Access --
- Add your organic group bundle to this field.
-- under Display Options --
- Check the boxes to display this link.

email_og

-- under Flag Access --
- Add your organic group bundle to this field ( you probably do not need anything else here)

At this point you should be able to subscribe/receive emails from a given organic groups. To test this you can:

  • Create an organic group
  • Subscribe to the group
  • Visit "/user"
  • Select the Subscriptions tab
  • You should see a table with
    | Group Name | Send Email | Unsubscribe |
  • To enable email, you will need to click on Send Email. You can use flag_rules to make "send email" a default selector, but currently users will need to manually choose to receive emails when subscribing.

If this all seems correct, you can now move on to creating a message architecture from "/admin/structure/messages/add".

Create the message "Group Content," with two message fields and save the message. One field will be the email subject and the other will be the email body.

Create three new fields on the message:

  • "Rendered Og Subject" -- machine name = field_rendered_og_subject) - text - Set "text processing" to filtered.
  • "Rendered Og Body" -- machine name = field_rendered_og_body - long text - Set "text processing" to filtered.
  • "Og ref" -- machine name = field_og_ref - entity reference - autocomplete

Create a Message view as table with the following fields:

  • Fields: User: User Name
  • Fields: Message: Rendered Og Body (Rendered Og Body) :
  • Fields: Message: Rendered Og Subject (Rendered Og Subject)
  • Filter by:Message Type: -- use the message type you created above.
  • Sort by:Message ID desc
  • Relationships:Message User uid <-- Add this relationship to User: User Name

Create a new module (example below is call sn):

sn/sn.info

name = Subscribe Notify 
description = Subscribe API for the Message and Message notify modules.  
core = 7.x

sn/sn.module

<?php

/**
 * Implements hook_node_insert() */

function sn_node_insert($node) { 
  $message = message_create(group_content'); 
  $wrapper = entity_metadata_wrapper('message', $message); 
  $wrapper->field_og_ref->set($node->og_group_ref['und'][0]['target_id']); 
  $options = array( 
    'rendered fields' => array( 
      'message_notify_email_subject' => 'field_rendered_og_subject', 
      'message_notify_email_body' => 'field_rendered_og_body', 
    ), 
  ); 
  $subscribe_options = array( 
    'subscribe message' => TRUE, 
    'email message' => TRUE, 
  ); 
  if (module_exists('message_subscribe')) { 
    message_subscribe_send_message('node', $node, $message, array('email' => $options), $subscribe_options); 
  } 
}

Make sure the line $message = message_create('MESSAGE_NAME'); of the module has the correct machine name of your message.

In the above module you can modify which type of message gets stored in the database. If you want a rendered subject and body, as this how-to outlines, you will want to make "subscribe message = FALSE". Having based this off of the message_notify_example module, my preference is to show messages in such a way that they contain all of the same information as email messages, with rendered fields. However, you can modify the view above to show the rendered message as one cell in the table, instead of showing the rendered subject and rendered body. It all depends on how you want your users to see messages on the site. Note: If you leave both $subscribe_options, 'subscribe message' and 'email message', set to true, your users will see two messages for each subscription, not ideal behavior. You should probably choose one or the other.

Enable the module.

Now you should be able to subscribe to a group, get emails about updated content, or you may choose to receive only on-site messages.

Comments

nubeli’s picture

I've tried this out. So far it hasn't completely worked but that might be because of something else wrong on the install. I'll need to test on a fresh install. So far the patches haven't been accepted but I thought I'd paste the URLs so someone can track them:

https://drupal.org/node/1983226
https://drupal.org/node/2205243
https://drupal.org/node/2126689

-------
Herb v/d Dool
Developer at Freeform Solutions

RKopacz’s picture

Has this been committed to the module so that it will work with OG? Can't seem to find that anywhere.

brad.bulger’s picture

installed all of this and applied the patches, but if i then go to my Account page, for each "og" flag i get an error that the "node" version is misconfigured or not installed. i have to enable subscribe_node and subscribe_og, email_node and email_og.

i don't really get the purpose of the subscribe flag, separate from the email flag. but given that this is not even close to being a complete solution, with so much custom code required, i guess i'll just end up rewriting that anyway.

j1ndustry’s picture

In the above approach, you must first create a node to subscribe to it (through the subscription tab on the /user page). Yet, the function "message_subscribe_send_message" is only called on node insert - before you're able to subscribe. You won't be subscribed at node insert so you'll never get the email notification.

While trying out this approach, I ran into a couple other problems. I ended up using a node subscribe flag because for me subscribing to the node was the same as subcribing to the group. The group was a node and they were always the same.

Here are some things you might want to try:

- programmatically subscribe the user before calling "message_subscribe_send_message" using something like this:

  //subscribe the user to the node
  global $user;
  $flag_subscribe = flag_get_flag('subscribe_og');
  $flag_subscribe->flag('flag', $node->nid, user_load($user->uid), TRUE);
  $flag_subscribe = flag_get_flag('email_og');
  $flag_subscribe->flag('flag', $node->nid, user_load($user->uid), TRUE);

- add a couple more options to the subscribe options.
I had to change from

  $subscribe_options = array( 
    'subscribe message' => TRUE, 
    'email message' => TRUE, 
  ); 

to

  $subscribe_options = array(
    'save message' => TRUE,
    'notify message owner' => TRUE,
    'subscribe message' => TRUE, 
    'email message' => TRUE, 
  ); 

Without these changes, the message was not getting saved, and the owner / creator of the message was never notified. Again, I didn't quite use this approach since in my case subscribing to the node served the same as subscribing to the group, but I did get it to work in a similar way.