Last updated April 24, 2008. Created on April 24, 2008.
Edited by scedwar. Log in to edit this page.

I use this to show a block:

1) only in a group;
2) only if the user has access (for example, block won't show if the node is unpublished, even if the user owns the node/group);
3) only in groups of a particular named type; and/or
4) only to group admins.


//check og module exists
if (module_exists('og')){

//check we've got a group, rights to view the group, 
// and of type "group_type" - change this to whichever group you want to restrict the block to
//or remove the condition entirely
if (($group = og_get_group_context()) && node_access('view', $group) && ($group->type == 'group_type') ) {

 //check current user is a team admin
 if (og_is_node_admin($group))  return TRUE;
 }
}

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

aschiwi’s picture

og_is_node_admin($group) is now og_is_group_admin($group)

<?php
//check og module exists
if (module_exists('og')){

//check we've got a group, rights to view the group,
// and of type "group_type" - change this to whichever group you want to restrict the block to
//or remove the condition entirely
if (($group = og_get_group_context()) && node_access('view', $group) && ($group->type == 'group_type') ) {

//check current user is a team admin
if (og_is_group_admin($group))  return TRUE;
}
}
?>
Sinan Erdem’s picture

I did a filtering for membership request type of a group (approved, invite-only etc.)

I just added ($group->og_selective == '2') to the if statement.

0 is for open, 1 is for moderated, 2 is for invite-only, 3 is for closed groups.

The final code was:

//check og module exists
if (module_exists('og')){

//check we've got a group, rights to view the group,
// and of type "group_type" - change this to whichever group you want to restrict the block to
//or remove the condition entirely
if (($group = og_get_group_context()) && node_access('view', $group) && ($group->type == 'group_type') && ($group->og_selective == '2')) {

//check current user is a team admin
if (og_is_node_admin($group))  return TRUE;
}
}
grasmash’s picture

I'd like to make a certain block visible to only members of group "Exhibitors"
However, I'd like this block to be visible on both group and non-group pages (so, anywhere on the site).

I've set the block to only show if the following php returns true:

//check og module exists
if (module_exists('og')){

//check we've got a group, rights to view the group,
// and of type "group_type" - change this to whichever group you want to restrict the block to
//or remove the condition entirely
if (($group = og_get_group_context()) && node_access('view', $group) && ($group->type == 'Exhibitors') ) {
return TRUE;
}
}

But it does not appear for any users. I'm assuming it's because it's missing an argument and og_get_group_context() is coming up without results.

Does anyone know how to remedy this problem?
Thanks

The techie formerly known as madmatter23
Grasmash.com - Drupal Tech Blog

nedwardss’s picture

Hey madmatter23,
After working on this for a while (had I think the same need as you), I came up with the following that seems to work:


//check og module exists
if (module_exists('og')){
//must pass the group id (aka gid) which is the nid of the group homepage (in my case 359)
  if(og_is_group_member("359")) { 
          return TRUE; 
  }
}

See also http://drupalcontrib.org/api/function/og_is_group_member/6
That's where I got the idea.

ydahi’s picture

here is what I have so far:

$node = menu_get_object();

//is this a node and not the subscribe to group page?
if(empty($node)) {
	return FALSE;
	} else {
		
		//is it the right node group?
		if ($node->type != 'course') {
		return FALSE;
		} else {
		
			global $user;
			$uid = $user->uid;
			
			//can we retrieve group context (optional)
			if (($group = og_context()) && (!empty($group))) {
			
				$group_type = $group['group_type'];
				$gid = $group['gid'];
			
				//does the third array exists (group admin)?
				if (($get_role = og_get_user_roles($group_type, $gid, $uid)) && (array_key_exists(3, $get_role))) {
					return TRUE;
				} else {
					return FALSE;
				}
			}
			
		}
	}

This does work however, only for site admin. Any other user, despite being the group admin, does not have the associative array ['3'].

Any help would be appreciated.

subhojit777’s picture

You can use og_user_access() to check if user is admin. However you have to assume that "administer group" permission is only given to group admin. If that is the case then you can use: og_user_access()

Regards,
Subhojit Paul