background image

Content tagged with: organic groups

Eric's picture

The Organic Groups Views Integration module comes with a bunch of useful views related to groups and their members. I assigned the "Recent members" block view to a region of my theme, and the block appeared on all group nodes and nodes that belong to a group. For the Drupal site I was working on, it was desired to only appear on the group nodes (not the nodes that belong to groups). No worries, you can always add block visibility code to determine when to show the block.

I went to the blocks admin screen (admin/build/block) and clicked configure for this block. Scroll down to the fieldset titled "Page specific visibility settings". I choose the radio option "Show if the following PHP code returns TRUE (PHP-mode, experts only)." and added the following PHP snippet:

<?php
if (function_exists('_MYMODULE_block_visibility_og_members'))
  return
_MYMODULE_block_visibility_og_members();
return
false;
?>

Next, I added a function in my module to handle the request.

<?php
function _MYMODULE_block_visibility_og_members() {
 
// lookup node id from query string
 
$path = drupal_lookup_path('source', $_REQUEST['q']);

 
// ensure the page is a node
 
if (substr($path,0,5)=='node/') {
   
// load node
   
$node = node_load(substr($path,5));

   
// check if node type is a group node
   
return og_is_group_type($node->type);
  }
  return
false;
}
?>

Now, the block only appears on group node types.

Eric's picture

In a recent Drupal implementation, we used the Organic Groups module to allow users in a certain role to add content to group nodes. On the content type edit screens, for "Organic groups usage", we chose "Standard group post (typically only author may edit)". Unfortunately, this text is a little deceiving. The OG module grants group administrators the ability to edit any node in the group, which was undesired for our situation.

In the og.module module file, the function og_menu_alter() overrides the normal access control of a user's ability to edit nodes:

<?php
function og_menu_alter(&$menu) {
 
// If og_access is disabled, we at least add back the edit tab for group admins to edit their posts.
 
$menu['node/%node/edit']['access callback'] = 'og_menu_access_node_edit';
 
$menu['node/%node/edit']['access arguments'] = array(1);
}
?>

Prior to og_menu_alter() being executed, the menu structure was:

[access callback] => node_access
[access arguments] => Array
    (
        [0] => update
        [1] => 1
    )

The above array structure relies on the node_access() function to determine if a user has permission to edit a node. One solution to this problem is to define code in a module to reset this menu structure:

<?php
function MYMODULE_menu_alter(&$menu) {
 
$menu['node/%node/edit']['access callback'] = 'node_access';
 
$menu['node/%node/edit']['access arguments'] = array('update',1);
}
?>

Now, group administrators no longer have permission to edit every content item in a group.

Eric's picture

I am working on an invitation system using organic groups and I needed the ability to programatically add a user to a (closed) organic group when they register for a new account. I tried to use the og_subscribe_user function, but it tests if the group allows registrations. In my situation the groups are closed, so I had to resort to the og_save_subscription function:

<?php
function MYMODULE_user($op, &$edit, &$account, $category = NULL) {
  switch (
$op) {
    case
'insert':

     
// in my example, I'm querying a database for this number:
     
$groupID = 'some number';

     
og_save_subscription($groupID, $account->uid, array('is_active' => 1));
      break;
  }
}
?>

Eric's picture

I recently created a view to show all the nodes that exist in a user's organic groups. I originally created the same view in Drupal 5 by adding the filter "OG: Post in User Subbed Groups" is equal to "Currently Logged In User". Unfortunately, I could not find the equivalent filter in Drupal 6 Views, so I decided to use arguments. Argument handling code is structured a differently in Drupal 6:

1) add a new argument
2) for "Action to take if argument is not present" choose "Provide default argument"
3) for "Default argument type" choose "PHP Code"
4) for "PHP argument code" I entered:

return MYMODULE_views_group_nids();

It's a personal preference of mine to NOT insert code into my database, so I added the previously called function into a module. IMHO, code belongs in subversion so it can be versioned properly, not in a database. Here's my function definition:

<?php
function MYMODULE_views_group_nids() {
 
// create SQL statement to look up all group nodes of the current user
 
$sql = "select nid from og_uid where uid = '%d' and is_active='1'";
   
 
// query database
 
$resource = db_query($sql, db_escape_string($GLOBALS['user']->uid));

 
// fetch results from resource
 
$results = array();
  while (
$row = db_fetch_array($resource)) $results[] = $row['nid'];

 
// rollup results, comma separated
 
$results = implode(',', $results);

 
// return result
 
return $results;
}
?>