Skip to Navigation

Drupal: Restrict a Block to User's Buddies in Panels

Printer-friendly version

The code below can be used to hide blocks displayed on a Panel page. I think we tried doing this with PHP block visibility but couldn't quite get it to work. Luckily, panels provides the hook_panels_panel_content_alter() for each block/content type you have in a container. The code below is an example of using the hook to display certain blocks only if the logged in user is a buddy of the user whose profile is displayed.

   1:<?php    2:/**    3: * Tests is the logged in user is a buddy of the specified uid    4: * @param integer user id    5: */    6:function user_is_buddy($uid)    7:{    8:    static $is_buddy;    9:    static $user_buddies;   10:    global $user; // logged in user   11:   12:    if (!isset($is_buddy[$uid]) && $user)   13:    {   14:        if (!isset($user_buddies))   15:        {   16:            // hold the authenticated user's buddies and   17:            // minimize queries   18:            $user_buddies = array_keys(buddylist_get_buddies($user->uid, 'uid'));   19:        }   20:   21:        // users are there own buddies   22:        if ($uid == $user->uid)   23:        {   24:            $is_buddy[$uid] = true;   25:        }   26:        else if(user_access('administer buddylist'))   27:        {   28:            $is_buddy[$uid] = true;   29:        }   30:        else if (!empty($uid))   31:        {   32:            $is_buddy[$uid]= in_array($uid, $user_buddies);   33:        }   34:        else   35:        {   36:            $is_buddy[$uid] = false;   37:        }   38:    }   39:   40:    return $is_buddy[$uid];   41:}   42:   43:// implementation of hook_panels_pane_content_alter   44:// http://groups.drupal.org/node/7678   45:function planet_connect_panels_pane_content_alter($content, $pane, $args, $context)   46:{   47:    // user profile customizations   48:    if ('user' == arg(0) && $profile_uid = (int) arg(1))   49:    {   50:        // don't display profile about stuff to non buddies, EVER   51:        // delta is diff for every user   52:        $is_buddy = user_is_buddy($profile_uid);   53:        if(!empty($content))   54:        {   55:            if ('About You' == $content->subject && false == $is_buddy)   56:            {   57:                $content->content = null;   58:            }   59:            // don't display activity stuff to non buddies, EVER   60:            // delta is diff for every user   61:            if ('Recent Updates' == $content->subject && false == $is_buddy)   62:            {   63:                $content->content = null;   64:            }   65:            // don't display activity stuff to non buddies, EVER   66:            // delta is diff for every user   67:            if ('My Groups' == $content->subject && false == $is_buddy)   68:            {   69:                $content->content = null;   70:            }   71:   72:            // our list of restricted block deltas   73:            $restricted_blocks = array(   74:                'blogs_user_id',   75:                'resources_user_id',   76:                'projects_user_id',   77:                'favorites_user_id',   78:                'interests_user_id'   79:            );   80:   81:            // don't display blogs to non-buddies   82:            if (in_array($content->delta, $restricted_blocks) && false == $is_buddy)   83:            {   84:                $content->content = '';   85:            }   86:   87:        }   88:    }   89:   90:    return $content;   91:}