Drupal Tutorials

Creating a buddy list system in Drupal

Anyone who has used a social networking site won’t have failed to notice the importance of having a buddy or friend list system to allow site users to connect with each other.
So in this simple tutorial I’m going to show you how to create a buddy list system in Drupal using the buddylist 1 module.

Which module(s) to use

As some of you may have noticed there are two versions of the buddylist module; version 1 and version 2 (buddy API) as well as the relatively new friend module for Drupal 6 and the User Relationships module, so it’s important you choose the module that suites you needs and site the best.
I choose version 1 of the buddy list module because I only wanted to deploy a simple buddy system on DrupalSN. Version 1 is very lightweight and integrates with the views and the activity modules as well as providing a built in notifications (email) system.
If you wanted to create a more complex buddy system with buddy shortest route (a la Facebook) and buddy privacy settings it’s worth looking at the buddylist2 and the user relationships module as they provide API’s that you can interact with. Both these modules provide all the functionality of the buddy list module and more but if you only want a simple system they can be overkill.

One last thing on the modules, I’m not saying any of these modules are better than each other as I haven’t fully tested them all, they all offer something slightly different so it’s down to you which one suites your site the best. For this tutorial I’ll be using version 1 of the buddy list module.

Buddylist Module Setup

You can download the latest version of the buddy list module I’m using at http://drupal.org/project/buddylist and the first thing you will need to download and install this module. Please refer to http://drupal.org/node/70151 for help with installing modules and themes.

Once you’ve installed the module the configuration settings can all be found at http://yoursite.com/admin/settings/buddylist and it only takes a few simple steps to get up and running.

1) General settings
For a traditional social network setup you should select “Yes” under “Require Approval”. This will mean users have to confirm buddy requests before they made official. ie. User A makes a request to user B and user B then has to decide if they want to accept user A’s buddy request.
Selecting “No” under “Require Approval” is more like a site user just keeping a list of contacts rather than buddies as no approval is required.

2) Profile page options and Buddylist block options
The settings in these fieldsets are to do with the display of buddies in user profiles and blocks. My personal preference is to ignore these and set up these displays using the views module as it gives you a bit more freedom, I’m planning to cover this in a future tutorial so keep checking the site for more on this.

3) email
Screen shotThis is one of the most important parts of the buddy list configuration. This is where you select when a user should be notified (emailed) about buddy requests and confirmations and also where you can input custom email content if you like. That said the default email content provided by the module is sufficient for most sites.

A classic buddy system like on Facebook will notify users when they have a new buddy request and when a request they made has been accepted, so that’s the setup I recommend. For this setup you should enable the bottom two options as per the screen grab on the right.

That’s the basic setup you need for your buddy system done.

Custom theming

The buddy list module offers a number of theme functions that you can override to fit your needs. For example all of the buddy add, remove, accept/reject links could be overridden using your theme to use icons instead of links as I’ve done on DrupalSN, or you could simple change the link text (see below).

I’ll briefly run over an example below but if you’d like to know more about how to overridden theme function you should check out one of my previous tutorials @ http://drupalsn.com/drupal-tutorials/drupal-theme-function-overriding.

So for this example I’ll quickly change the default “Add to buddy list” link on line 757 of the buddylist module to read “Add USERNAME to my buddy list” where USERNAME is the name of the user to be added.

Old code:

<?php
function theme_add_to_buddylist_link($buddyuser) {
  return
l(t('Add'), 'buddy/add/' . $buddyuser->uid, array('title' => t('Add User')), drupal_get_destination(), NULL, FALSE, TRUE);
}
?>

New code to add to your themes template.php file:

<?php
function phptemplate_add_to_buddylist_link($buddyuser) {
 
$title = t('Add !username to my buddy list', array('!username' => $buddyuser->name));
  return
l($title, 'buddy/add/' . $buddyuser->uid, array('title' => $title), drupal_get_destination(), NULL, FALSE, TRUE);
}
?>

You could use this same principle with all the theme functions offered by the buddy list module, check out from line 740 in the buddy list module or do a code search (for theme_) to view the available theme functions.

Taking it further

By now you should have your basic buddy list system up and running, so I’m briefly going to take a look some other contrib. modules you could look at in order to take your buddy system to the next step.

1) Invite module
This module does exactly as the name suggests; it invites users to a site and it integrates with the buddy list module (see the contrib. folder within the buddy list module folder) so that invited users automatically become buddies of the person who invited them to the site.
It’s a great way of promoting your site as well as creating buddy connections.

2) Userpoints
This collection is collection of modules that run off the userpoints API that allows you to give users points or credits when they complete tasks on your site. So you could offer users points for adding buddies and then reward the users with a high amount of points.

3) Activity
The Activity module keeps track of the things people do on your site and provides mini-feeds of these activities and it integrates with the buddy list module so you could provide users with their buddies “activity” on your site.

4) CCK Field Privacy
This module allows users to restrict access to certain fields on nodes they have created, so users could use it restrict access to certain node information to just their buddies.
There is also some development happening and patches available for the profile privacy module so users can restrict access certain profile fields to just their buddies. View the thread @ http://drupal.org/node/156082.

Well, I hope that this tutorial has helped you get your buddy list system up and running and hopefully it’s given you an insight into the options that are available to you. I know this wasn’t the most practical tutorial but I hope you’ve found it interesting, as always please leave your comments and suggestions below.

Thanks for reading!

Commenting on this Tutorial is closed.

Categories:

Comments (20)

hychanhan's picture

Excellent! :p

droople's picture

What’s your views code to show user pictures in buddylists?

All how do you place,add to buddies icon onuser views?

thanks

DrupalSN's picture
Hi, Here's the views code to create the buddylist block on user profiles:
  $view = new stdClass();
  $view->name = 'buddylist_block';
  $view->description = 'Block of buddies of the current user';
  $view->access = array (
);
  $view->view_args_php = 'if (arg(0) == \'user\' && is_numeric(arg(1)) && arg(2) == \'\') {
  $args[0] = arg(1); 
}
else {
  global $user;
  $args[0] = $user->uid;
}
return $args;';
  $view->page = FALSE;
  $view->page_title = 'Buddylist';
  $view->page_header = 'Buddies of the current user<br />';
  $view->page_header_format = '1';
  $view->page_footer = '';
  $view->page_footer_format = '1';
  $view->page_empty = '';
  $view->page_empty_format = '1';
  $view->page_type = 'table';
  $view->url = 'buddies';
  $view->use_pager = TRUE;
  $view->nodes_per_page = '10';
  $view->block = TRUE;
  $view->block_title = 'Buddies';
  $view->block_header = '';
  $view->block_header_format = '1';
  $view->block_footer = '';
  $view->block_footer_format = '1';
  $view->block_empty = 'No buddies to show';
  $view->block_empty_format = '1';
  $view->block_type = 'teaser';
  $view->nodes_per_block = '5';
  $view->block_more = TRUE;
  $view->block_use_page_header = FALSE;
  $view->block_use_page_footer = FALSE;
  $view->block_use_page_empty = FALSE;
  $view->sort = array (
    array (
      'tablename' => 'node',
      'field' => 'created',
      'sortorder' => 'DESC',
      'options' => 'normal',
    ),
  );
  $view->argument = array (
    array (
      'type' => 'buddylist_buddies',
      'argdefault' => '2',
      'title' => '',
      'options' => '',
      'wildcard' => '',
      'wildcard_substitution' => '',
    ),
  );
  $view->field = array (
    array (
      'tablename' => 'node',
      'field' => 'title',
      'label' => 'Name',
      'handler' => 'views_handler_field_nodelink',
      'options' => 'link',
    ),
  );
  $view->filter = array (
    array (
      'tablename' => 'node',
      'field' => 'status',
      'operator' => '=',
      'options' => '',
      'value' => '1',
    ),
    array (
      'tablename' => 'node',
      'field' => 'type',
      'operator' => 'OR',
      'options' => '',
      'value' => array (
  0 => 'usernode',
),
    ),
  );
  $view->exposed_filter = array (
  );
  $view->requires = array(node);
  $views[$view->name] = $view;

The view uses a usernode teaser list which then allows me to manaully setup imagecache for the user pictures in a node-usernode.tpl.php file.

The "add to buddies" icon is done using the same technique highlighted under the "Custom Theming" header above, but instead of change the text i added an icon similar to below:
<?php
function phptemplate_add_to_buddylist_link($buddyuser) {
 
$title = t('Add to my @buddylist', buddylist_translation());
 
$attr = array('title' => $title);
 
$picture = path_to_theme() .'/images/icons/buddy/user_add.png';
 
$img = theme('image', $picture, $title, $title, '', FALSE);
  return
l($img, 'buddy/add/' . $buddyuser->uid, $attr, drupal_get_destination(), NULL, FALSE, TRUE);
}
?>


Hope that helps
Tom

Thanks Tom. I’m trying to have the buddy’s block display like what you have on this site i.e. A’s username underneath the A’s pic and have several pics in one row. Right now I am only able to display a buddy pic per row and can’t display the username underneath the pic.

Eminent Technology solutions's picture

wat are the module can i use to build the social network… Please suggest…...

Eminent Technology solutions's picture

wat are the module can i use to build the social network… Please suggest….. and i am using Drupal 6.3… Plz reply as soon as possible….

droople's picture

Thanks

dtabach's picture

Hello,
You said “you could offer users points for adding buddies and then reward the users with a high amount of points”, but I looked everywhere to find out how to achieve this functionality without luck. Can this be done with Buddylist 5.x-1.1-beta and Userpoints 5.x-3.7, or other modules/versions are required for this functionality?

DrupalSN's picture

Hi dtabach,
sorry for the delay in replying.
yeh i’m stuggling to see the module too, but i’m sure i’ve read about it somewhere. the User Points Contributed modules page (http://drupal.org/project/userpoints_contrib) has the buddylist module listed as providing Userpoints integration, but no sign of a module anywhere.
The search goes on!
Thanks
Tom

dtabach's picture

Yes, I also have seen this info in User Poitns Project page, I even created an issue about that, but did not find anything yet. At least I’m not the only one who is looking…

stellarvisions : communication architects's picture

Thanks for this info.
Going to be trying and implementation for a client.
The privacy issue is a big thing for members so I’m glad you mentioned the two levels of module use.

I’m sure you will be getting feedback on our efforts.

Just what we needed to get started.

—-
Stella Gassaway
principal

STELLARVISIONS : communication architects

gallamine's picture

FYI, the “Friends” module you list for Drupal 6 is now listed as abandoned. Oh well. Looks like 6ers will have to use “User Relationships”. Anyone else have suggestions?

nimzie's picture

I’m looking to try and add an “add / remove buddy” block and a listing of the currently viewed users’ buddies on a page, and I found this tutorial. We’re using different modules to get the job done though (you are using usernode and I am using userprofile).

I’m working on user_profile.tpl.php and have brought in a bunch of views to do this already.

I can’t seem to bring in the buddies.

Can anyone offer some assistance on how to adapt this tutorial to this need?

I want to show buddies on a user’s page. Not just my own. Any that I visit.
In my tpl.php file, I am operating with $account->uid as the user id for the user.

Thanks for any help. Great site. Great resources :)
Cheers,

Adam

DrupalSN's picture

Hi Adam,
Welcome to DrupalSN, glad you like the site!
You should be able to use the views export code i posted above (http://drupalsn.com/learn-drupal/drupal-tutorials/creating-a-buddy-list-...), this code creates a view with a block that shows a users buddies on their profile page.
Once you’ve imported the view, go to the blocks admin area and assign the “buddylist_block” block to a region on your user_profiles and that should do it!

If you want to embed the view into your user_profile.tpl.php then you could add the following code directly into your user_profile.tpl.php:

<?php
$limit
= 6; //number of buddies to show in block
$view = views_get_view('buddylist_block');
echo
views_build_view('block', $view, array(), false, $limit);
?>

Note this is for Drupal 5.

Give it a try!
Thanks
Tom

nimzie's picture

Awesome, I’ve got this plugged in and did a little bit of tuning. Now I have to figure out which function to override to override the teaser view drawing of the buddy itself.
Another thing.
In this view, how would I be able to include some information as to if the user is online or not.
Thanks for your help!
SN with Drupal .. coming up soon… I’ll post up what I’m working up once we’re ready to give er. It’s a pet proect, but a time taker.

You have made this seem simple enough! i’m going to try and set up a buddy list on my how to potty train a puppy and piano tutorials sites as there is likely to be an active community there.

Great little tutorial. Thanks. Great site too with lots of resources.
Life Insurance No Physical Term Life Quotes

This article is a package of many information which is really very helpful for every body in all works.
tramadol online
buy tramadol
tramadol