Skip to Navigation

Drupal: Create a Block to Display a User's Organic Groups

Printer-friendly version

I was a bit surprised that, at least in D6, the organic groups module doesn't supply a block to show a user's group memberships. We had to display such a block on a client site recently and defined the box ourself. First, define a new block via hook_block():

   1:<?php    2:function forumone_block($op = 'list', $delta = 0, $edit = array())    3:{    4:    switch ($op) {    5:        case 'list':    6:            $blocks[0]['info'] = t("OG: Show a profile's group membership.");    7:    8:            return $blocks;    9:        break;   10:   11:        case 'view':   12:            switch ($delta)   13:            {   14:                case 0:   15:                    $block['subject'] = t('My Groups');   16:                    $block['content'] = forumone_users_og();   17:                break;   18:            }   19:            return $block;   20:        break;   21:    }   22:} 

The forumone_users_og function takes advantage of the fact that a user's group memberships is a property of the object returned by user_load(). This block assumes that the uid comes from the url of the current page.

   1:<?php    2:function forumone_users_og()    3:{    4:    // should be the profile the current user is looking at    5:    $uid = (int) arg(1);    6:    if ($uid)    7:    {    8:        $usernode = user_load(array('uid' => $uid));    9:   10:        if (isset($usernode->og_groups))   11:        {   12:            foreach ($usernode->og_groups as $ogid => $og)   13:            {   14:                $items[$og->nid]['data'] = l($og['title'], 'node/' . $og['nid']);   15:            }   16:            return theme('item_list', $items);   17:        }   18:    }   19:}   20: