I'm testing a Drupal Commons 3.3 site and I'd like to configure it so new users are automatically subscribed to receive email notifications for any activity when they are added to a group.

Is there some code I can modify to do this?

thanks

CommentFileSizeAuthor
#20 2079833-follow-group-20.patch2.58 KBezra-g

Comments

stevehutchison’s picture

Status: Active » Closed (fixed)

ok - got it working. Added this function to a custom module.

function cm_overrides_og_membership_insert(OgMembership $og_membership) {
// Get account and groups
// Only check for common_groups
if ('og_user_node' === $og_membership->field_name && 'node' === $og_membership->group_type
&& 'user' === $og_membership->entity_type) {
// Get flow flag + email flow flag
$flag = flag_get_flag('commons_follow_group');
$email_flag = flag_get_flag('email_group');
// Get account
$account = user_load($og_membership->etid);
// Flag user
$flag->flag('flag', $og_membership->gid, $account, TRUE);
$email_flag->flag('flag', $og_membership->gid, $account, TRUE);
}
}

ezra-g’s picture

Title: Enable notifications by default when a user joins a group » Have user auto-follow a group when she becomes a member
Category: support » feature
Status: Closed (fixed) » Active

This seems like a great feature request.

daniel-san’s picture

This would be a very helpful feature. I was just trying to figure out how to do this and came across your post. Thanks.

Scenario (in case it helps people understand why this would be helpful)

  • Setting up a discussion forum for a group of county-wide leaders.
  • Group administrator will add them to the group and they should automatically begin receiving notifications regarding posts made to their group.
  • Going to take away all things they will not need: likes, share to twitter, facebook, etc.
  • Make it dead simple so that they can login and start posting and responding to posts.
daniel-san’s picture

accidentally duplicated when saving initial comment.

breity’s picture

+1! This is a great request. And thanks a lot for override function @stevehutchison! I'm using it for now in my installation.

daniel-san’s picture

Tested out custom module in comment #1 and it worked perfectly. Whether or not this feature is added to Drupal Commons, it was a great help and I appreciate you taking the time to post this for others.

For those that need info for implementing this into their own module, you need to create a custom module and use the name of the module at the beginning of the function (og_membership_insert).
Therefore, my module is name is "commons_autofollow" and so I changed the function at the beginning to:

function commons_autofollow_og_membership_insert(OgMembership $og_membership)

Just in case someone needs the extra info on how to use it. Have a great day.

Dan

WebSinPat’s picture

I am using the function in #1 and it seems like it's working for me. thanks.

Just wanted to note a few points about how the timing of a user becoming a follower plays out in a couple of the scenarios:

- when a user becomes part of an open group, i suppose the og_membership is created when a user posts to the group and thus becomes a contributor. At which point, per the auto-follow function, they become a follower of the group. Depending on what the open group is, I can foresee situations where someone might want to just post once or twice but not follow everything in the group, since they might not see themselves as "joining" but rather just contributing a post. In which case they can unfollow themselves easily enough.
- in the case of joining a moderated group initiated by the user, the og_membership is created when the user requests membership which may not be at the same time the membership is approved. Per the auto-follow function, they become followers of the group immediately upon requesting membership. Which probably makes an equal amount of sense as the alternative of tying following to active status.
- for a moderated or private group when a user is added to a group by an admin, the user becomes a follower at the same time, which is just as I would expect.

In case this is helpful for anyone down the line wanting behavior similar to this feature request but needing to precisely control the timing of becoming a follower.

behoppe333’s picture

+1 for this being a feature. Thanks all for posting this code and the explanation.

Based on @websinpat's explanation, I suggest one improvement: In the case of user requesting membership in a moderated group, auto-follow should happen when the request is approved, not when the request is made.

I have a moderated group with content hidden from non-members, and I want auto-follow, and I don't want auto-follow to open up an email security hole that allows non-members to see group content. (I.e., Pending members are definitely not members.)

japerry’s picture

Version: 7.x-3.3 » 7.x-3.x-dev
Assigned: Unassigned » japerry
Issue summary: View changes

This should be easy to do. Adding it to my queue

behoppe333’s picture

Per @daniel-san #6 I made custom module commons_autofollow and that works on my test site. (Thanks!) But when I enable the module on my production site I get a systemic "no tokens available" error which I describe here. When I disable my custom module, then tokens work again. I am not a programmer. This is my first-ever custom module. Any newbie-oriented advice appreciated.

WebSinPat’s picture

@behoppe333, i'm pretty newbie too, but took a quick look, and i'm sorry but have no idea what might be going on. it's weird because the autofollow module code doesn't even seem to touch tokens. And in my case the code seems to work fine on both my dev and live sites. My only suggestions would be general ones about tracking down what might differ between your live and test sites, like: sever config differences if they are on different hosts, different modules enabled/disabled, any other code/patch differences, any config changes you made one place and not the other, etc. sorry, not very helpful.

tleavitt’s picture

It seems that the same thing could be accomplished using Rules, rather than a custom module:

{ "rules_og_auto_follow_on_approve" : {
    "LABEL" : "OG Auto-Follow on Approve",
    "PLUGIN" : "reaction rule",
    "TAGS" : [ "og organic group follow" ],
    "REQUIRES" : [ "rules", "flag", "og" ],
    "ON" : [ "og_user_insert" ],
    "IF" : [
      { "data_is" : { "data" : [ "account:status" ], "value" : "1" } },
      { "entity_is_of_type" : { "entity" : [ "og-membership:group" ], "type" : "node" } }
    ],
    "DO" : [
      { "variable_add" : {
          "USING" : { "type" : "node", "value" : [ "og-membership:group" ] },
          "PROVIDE" : { "variable_added" : { "group" : "Group" } }
        }
      },
      { "flag_flagnode" : {
          "flag" : "email_group",
          "node" : [ "group" ],
          "flagging_user" : [ "account" ],
          "permission_check" : 0
        }
      },
      { "flag_flagnode" : {
          "flag" : "commons_follow_group",
          "node" : [ "group" ],
          "flagging_user" : [ "account" ],
          "permission_check" : 0
        }
      }
    ]
  }
}

Is there any reason not to take such an approach, from a Drupal Commons perspective?

FunkMonkey’s picture

tleavitt: I was looking at doing roughly the same thing with Rules as well. We want to have everyone that creates an account to automatically Follow a couple of groups (School District News for example).

I imported the Rule you have above but it is way over my head. Do you know how I might adapt that to just Follow a specific group (or a few groups) when a user is added or edited? I've only done very simple things with Rules.

In the Conditions, the Status = Active makes sense to me. Not sure I understand the need to make sure the group is a node in the second one though. I sorta kinda think I understand part of the Actions section.

Could you provide a little more info on that Rule and how it works? Thanks.

tleavitt’s picture

@FunkMonkey -

I created the rule above using the Rules UI module. The precise text I posted is a bit above my head too, in that form. ;-)

I included validation that the group is a node because that's what the flag is for, and because it seemed to be included in the originally-posted code. However, this may be unnecessary - I'm pretty new to Drupal, and don't have a great handle on the object model yet.

The rest of the rule:
* Creates a new variable of type "node" equal to the new og_membership's group - I couldn't dig any deeper in the group's properties using Rules UI, so this was necessary.
* Sets the email_group and commons_follow_group flags, same as the module in comment #1

To answer your question regarding following a specific group or groups... I suspect that a similar approach could be used with the "Fetch entity by ID" or "Fetch entity by attribute" actions available in Rules UI, instead of setting the variable equal to the group for the newly-created og_membership.

Does that make any sense?

FunkMonkey’s picture

@tleavitt: Awesome! Thanks. I was also having trouble getting what I needed in the UI. Your solution makes much more sense now. I'll give the 'Fetch entity" stuff a try. I didn't read the module thoroughly.. I think that's why I didn't understand setting the email_group and commons_follow_group flags.

So, yes, that makes sense. And it probably a better solution than a custom module since it can be adapted easily for similar uses cases and can be exported.

WebSinPat’s picture

just wanting to bump this and see the status with respect to an Official Commons Solution. I'm using a custom function as suggested in #1, which is working fine, though there are a couple of timing quirks not worked out yet as suggested in #7 and #8. I haven't tried the Rules-based solution yet to see if that handles the corner cases better.
Anyway this just seems like a pretty standard feature that I think should get into the commons codebase one way or another.

ezra-g’s picture

Assigned: japerry » Unassigned
Issue tags: +commons 7.x-3.9 radar

Our 3.9 radar is pretty full at this point, but I'd really like to see this issue resolved, so adding to our radar in the hopes we can fit it in for the next release. If someone can file a patch, that would increase the chances of this being included in 3.9 :).

ezra-g’s picture

Priority: Normal » Major

This seems like behavior that most users would reasonably expect, so bumping to major priority.

ezra-g’s picture

Assigned: Unassigned » ezra-g
ezra-g’s picture

Status: Active » Needs review
StatusFileSize
new2.58 KB

Here's a patch for this functionality which takes into account:

- Unfollowing a group when a membership is deleted or changes status from "approved".
- Enforcing membership status (eg, blocked, pending, approved) with flagging and unflagging.
- Memberships being updated (eg, when going from one status to another)

It would be great to get a review here :).

WebSinPat’s picture

the patch in #20 works like a dream for all the different scenarios I tried out. :)

ezra-g’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.