Hi!
I would like to post messages in an Organic Group (in php) and that the message should look different if the user has subscribed to the Group or not. How can I find out if the logged in user has subscribed to a Group or not?

This is roughly the post I want to do in e.g. a group called 'groupX':

If logged in user != member of the groupX {
print 'click here to subscribe'; }
else if logged in user == member of the groupX {
print 'click here to see other user in this group'; }

I don't know how to get the statements in: '== member of the groupX'. How do I know if a user has subscribed to a specific group or not?

Thanks!
elLoco

Comments

cwgordon7’s picture

The following example assumes a group node called $group.

<?php
global $user;
if (in_array($group->nid, $user->og_groups) {
  print t('Click here to subscribe');
}
else {
  print t('Click here to see other user in this group');
}
?>

---
If you have any trouble creating, configuring, or adding features to your Drupal website, I can help!
You can contact me through my personal contact form.

elLoco’s picture

Sorry for being a beginner....
In your example the name is 'group' and the variable is '$group'. If I have a long group name what will my variable be? For example: if I name a group 'my new group' what will the $ variable be? $my_new_group? or the node number?

Thanks again!

elLoco’s picture

This is how I solved it:

global $user;
if ($user->og_groups['138']['nid'] == 138) {  //If user has subscribed to the group node/138
  print t('Click here to see other user in this group');
}
else {
  print t('Click here to subscribe');
}

My group is in 'node/138'.

Please correct it, if it is a bad solution!
Thanks!

cwgordon7’s picture

You misunderstood me. I thought that you would already be in a situation where you loaded the appropriate group node into a variable called $group (regardless of what the node title is).

Use the node_load($nid) function if you haven't loaded the node. Consult api.drupal.org if you have trouble.

-cwgordon7
---
If you have any trouble creating, configuring, or adding features to your Drupal website, I can help!
You can contact me through my personal contact form.

elLoco’s picture

Thank you! Sorry for my misunderstanding.
elLoco

johnhanley’s picture

I just encountered this issue today and the correct syntax to see if a user is a member of a particular group is as follows:

    global $user;
    // get fully-load user object including og_groups array
    $account = user_load(array('uid' => $user->uid));

    // does the nid for the group exist in the og_groups array
    if (isset($account->og_groups[$node->nid])) {
      // yup, looks like we've got ourselves a member
      // do something that only members can see
    }

-------------------------------------------------------

"If you don't read the newspaper you are uninformed;
if you do read the newspaper you are misinformed."
-- Mark Twain