0

I'm using D7 Organic Groups to create custom member profiles such that when a new user registers, a rule creates a group with their name (/member/firstname-lastname) and redirects them to their group's home page where there is a "build-profile" panel using quicktabs to guide them through the 7 steps of building a complete profile. The build panel is visible until such time as they complete the final step at which point they are autoassigned a new role which uses a different page layout sans the build panel.

This all works well. When users browse for other users they are actually directed to the associated /member/firstname-lastname page for the user whose profile they are viewing.

Here is the issue I'm now having. I need to make it so that when a user "follows" another user (joins or subscribes-to their group) I want the owner of the group being followed to automatically "follow" (join or subscribe-to) their group.

This is really only doable because each member only "owns" or is admin to a single group, so it should be relatively easy to say - if user-a (owner of group-a) joins group-b (owned by user-b) then user-b joins group-a.

I'm just not sure programatically how to do this. Can anyone help me with a code snippet to do this?

0

I'm going to assume that you are using the 7.x-1.x branch of Organic groups. I believe there are some major changes in the 2.x branch - so this solution may/may not work with that.

// Assuming your group is of the entity type "node"
// and the name of your bundle is "group". A node bundle
// is the unique name of the content type which is a group
// type as well.
// Say $follower is the follower user entity and 
//     $followee is the followee user entity
$opts = array(
  ':uid' => $follower->uid
  ':type' => 'group'
);
$gnid = db_query('SELECT nid FROM {node} WHERE type=:type AND uid=:uid', $opts)->fetchField();
if ($gnid) {
  $group = og_get_group('node', $gnid);
  og_group($group->gid, array(
    'entity type' => 'user',
    'entity' => $followee,
    'state' => OG_STATE_ACTIVE
  ));
}
else {
  watchdog('my_mod', 'Follower (uid=!uid) does not have a member group', array(':uid' => $follower->uid), WATCHDOG_ERROR);
}

Still, own (group manager) and is admin of are two different things, at least in OG parlance. Because a user may have the "administrator user" role in many groups. If you mean is admin of, the lookup of the node table above may have to be changed by doing a join with {og} and {og_users_roles}.

  • Amarnath - this is awesome. I've been out all day and am just now seeing this for the first time. I'm going to implement this in the morning and will provide feedback. In this particular case no other user than the owner (group manager) will have administrator privileges. Thanks again! – Terry Jan 11 '13 at 1:16
  • I'm trying to decide where this code should live. I'm going to explore putting it into a rule. If you have a better suggestion please let me know. Thanks again! – Terry Jan 11 '13 at 1:22
  • A rule sounds just about right although frankly I haven't worked with it before. Alternatively, if your Follow button has a menu callback, this could go in there. If your Follow button is a flag, then this could go into hook_flag(). – Amarnath Ravikumar Jan 11 '13 at 12:55
  • I can't find where to post the php script above. I have been playing around with Rules and there is a rule with Event: User has become a group member and there is an Action: Subscribe user to group where you can define the "user who will be subscribed" as: site:current-group:manager - which essentially says the manager of the current group will be subscribed to whatever Group data selector you choose. I know I'm on the right track - I just can't figure out which data selector will equal the "current logged-in user's managed OG." site:current-user:og-membership:0:group didn't work. Ideas?? – Terry Jan 13 '13 at 2:36
  • I REALLY feel like this should work - site:current-user:og-membership:0:group:manager:og-membership:0:group as the data selector - but it doesn't add the current logged-in user to the group of the manager of the group triggering the rule. – Terry Jan 13 '13 at 2:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.