add pictures to buddylist page?

zottmann - August 5, 2007 - 15:25
Project:Buddylist
Version:5.x-1.0
Component:User interface
Category:support request
Priority:normal
Assigned:Unassigned
Status:active
Description

Hi!
Is there a way to add the buddies pictures to the default buddylist view? I've managed to to that with the "buddies" view provided by the module, but, in order to use this as a replacement to the default "buddylist" view I should also rebuild the tabs to groups and contribution.

I'm using Drupal 5 and the official release of buddylist.

Thanks in Advance,
Carlos.

#1

dldege - August 6, 2007 - 14:50

I have just a few days ago checked in a change to buddylist.module to allow better theming of the buddylist page (?q=buddylist). It would be quite easy to implement user pictures now.

http://drupal.org/node/163104

Here is the function

<?php
function theme_buddylisting($account, $buddies, $buddies_per_page) {
 
drupal_set_title(t('%username\'s @buddylist', array('%username' => $account->name) + buddylist_translation()));

 
$header = array(t('@buddy', buddylist_translation()), t('online'));

  if (
count($buddies)) {
    foreach (
$buddies as $buddy) {
     
$rows[] = array(theme('username', user_load(array('uid' => $buddy['uid']))), theme('buddylist_online', $buddy['online']));
    }
   
$output .= theme('table', $header, $rows);
  }
  else {
   
$output .= variable_get('buddylist_empty_text', t(buddylist_empty_text_default(), buddylist_translation()));
  }

 
$output .= theme('pager', NULL, $buddies_per_page);
 
  return
$output;
}
?>

As you can see it would be very easy now to add in the user picture in each row of the table.

#2

zottmann - August 7, 2007 - 02:52

Hi!!

Thanks for the update!! I'll try that!!

I'm also finishing the translation of the buddylist module to brazilian portuguese. Should I send it to you in order to publish it?

Regards,
Carlos.

#3

dldege - August 7, 2007 - 16:19

You can create a new issue for the translation and attach the relevant files. Thanks,

Let me know if the theme hooks work out for you.

#4

zottmann - August 8, 2007 - 03:01

OK!

I'll try to review the translation by the end of the week in order to send it to you. By the way, I didn't find a way to translate the text that resides in the database and is used for things like the body of e-mail messages, block titles, etc ... How can these texts be translated?

Thanks for the tip on the buddylist theming as well. I didn't have time to try it yet, but surely I will.

Thanks again,
Carlos.

#5

zottmann - August 21, 2007 - 02:47

Hi!

I've just changed the theme_buddylisting function like below:

$header = array(t('picture'), t('@buddy', buddylist_translation()), t('online'));

if (count($buddies)) {
foreach ($buddies as $buddy) {
$rows[] = array(theme('picture', user_load(array('uid' => $buddy['picture']))), theme('username', user_load(array('uid' => $buddy['uid']))), theme('buddylist_online', $buddy['online']));

The header is beeing displayed ok, but the user pictures are not beeing shown.

Could you tell me what I'm missing?

Thanks in advance,
Carlos.

#6

VM - August 21, 2007 - 02:52

should post php using <code> tags so we can see all of it.

#7

VM - August 21, 2007 - 02:53

another note: this was rolled into the drupal 5 branch already per the other node posted. Thus I don't believe you need to add the function any longer. Please read the node posted above by dldege

#8

dldege - August 21, 2007 - 21:44

you want to call theme('user_picture', user_load(array('uid' => $buddy['uid']))) is the problem I'm seeing on quick glance.

#9

zottmann - August 22, 2007 - 00:46

Hi!
Thanks for the replies, dldege and VeryMisunderstood!

Dldeges' tip did the trick, but I still have one question. I've searched through the buddylist and user tables, but did not find any "user_picture" field. How does it work? Where does this field come from?

Anyway, thanks for the tips, and the next time i'll post code with code or ?php tags.

Regards,
Carlos.

#10

VM - August 22, 2007 - 00:52

are user_pictures enabled in administer -> user settings ?

#11

dldege - August 22, 2007 - 15:02

The user picture is stored in the $user->picture variable when user pictures are enabled. Calling theme('user_picture', $user) ends up calling into the theme layer to determine how the picture is actually formatted and rendered in the HTML.

Here is the default handler for theme('user_picture', $user)

http://api.drupal.org/api/function/theme_user_picture/5

#12

yngens - August 23, 2007 - 05:27

subscribe

#13

zottmann - August 24, 2007 - 03:10

Hi!

Thanks for the explanation... I'm not a php programmer, so I'll need to study it a little in order to fully understand how the api works, but thanks anyway!

The important thing is that I was able to get the user pictures shown in the buddylist page...

Thanks again!
Carlos.

#14

vrc3 - September 21, 2007 - 03:08

I altered the function buddylist_buddylisting_page

I changed:

$rows[] = array(theme('username', user_load(array('uid' => $account->buddy))), theme('buddylist_online', $online));

to
$rows[] = array(theme('buddypic', user_load(array('uid' => $account->buddy))), theme('buddylist_buddyinfo',user_load(array('uid' => $account->buddy)) ));

and

$output .= theme('table', $header, $rows);

to
$output .= theme('table', NULL, $rows);

Then I added this to my template.php:

function phptemplate_buddypic($object) {

  $output = "<div class='buddypic'>".theme_user_picture($object).theme_username($object)."</div>";

  return $output;
}

function phptemplate_buddylist_buddyinfo($account) {
  return basic_info($account);
}

function basic_info($account){

  // COPIED FROM user_view
  $fields = array();
  foreach (module_list() as $module) { //probably want to pare down the list
    if ($data = module_invoke($module, 'user', 'view', '', $account)) {
      foreach ($data as $category => $items) {
        foreach ($items as $key => $item) {
          $item['class'] = "$module-". $item['class'];
          $fields[$category][$key] = $item;
        }
      }
    }
  }

  // Let modules change the returned fields - useful for personal privacy
  // controls. Since modules communicate changes by reference, we cannot use
  // module_invoke_all().
  foreach (module_implements('profile_alter') as $module) {
    $function = $module .'_profile_alter';
    $function($account, $fields);
  }

  $output = "<div class='buddyinfo'><dl>";

  // Location first
  $location = $fields["Location"][0];
  if (isset($location['value'])) {
    $output .= '<dt class="'. $location['class'] .'">Location:</dt>';
    $output .= '<dd class="'. $location['class'] .'">'. $location['value'] .'</dd>';
  }

  $profile = $fields["Profile"];

  // Age
  if (isset($profile['profile_birthday']) && !$account->profile_hide_age) {
    $year_diff = date("Y") - ($account->profile_birthday{year});
    $month_diff = date("m") - ($account->profile_birthday{month});
    $day_diff = date("d") - ($account->profile_birthday{day});
    if ($month_diff < 0)
      $year_diff--;
    elseif ($month_diff == 0 && $day_diff < 0)
      $year_diff--;

    $output .= '<dt class="'. $profile['profile_birthday']['class'] .'">Age:</dt>';
    $output .= '<dd class="'. $profile['profile_birthday']['class'] .'">'. $year_diff .'</dd>';
  } 

  // First few values of profile, note that this overrides the
  //"weight" specified by profile module. gender, birthday,
  //relationship, orientation
  $fieldnames = array("profile_gender","profile_relationship","profile_orientation");
  foreach($fieldnames as $fieldname) {
    if (isset($profile[$fieldname])) {
      $output .= '<dt class="'. $profile[$fieldname]['class'] .'">'. $profile[$fieldname]['title'] .':</dt>';
      $output .= '<dd class="'. $profile[$fieldname]['class'] .'">'. $profile[$fieldname]['value'] .'</dd>';
    }
  }

  // Now the buddy actions
  $buddy = $fields["Buddy List"][1];
  if (isset($buddy['value'])) {
    $output .= '<dt class="'. $buddy['class'] .'"></dt>';
    $output .= '<dd class="'. $buddy['class'] .'">'. $buddy['value'] .'</dd>';
  }

  $output .= "</dl></div>";

  return $output;

}

NOTE: This assumes the existence of certain profile fields.

You can see the result here: www.beerreviewjournal.com, but you have to register to see buddylists...

#15

jenlampton - October 9, 2007 - 22:58
Title:Is there a way to add the buddies pictures to the default buddylist view?» add pictures to buddylist page?

Though this is very helpful, it didn't answer the question of how to add the pictures to the default view supplied with the module. This answers the question of how to add the photos to the 'buddylist' page. I've renamed this issue, and I will open a new one to address the previous question.

Jen

#16

dldege - October 15, 2007 - 15:25

In the current HEAD version - not the released version I have added a theme function to allow for complete control over the buddylist page - ?q=buddylist.

The process is really quite simple.

  • 1. Locate the function function theme_buddylisting($account, $buddies, $buddies_per_page) in buddylist.module - copy this whole function to the clipboard.
  • 2. In your theme create or open template.php and paste the function into it - rename it to mythemename_function theme_buddylisting($account, $buddies, $buddies_per_page). For example, I'm using the hunchbaque theme so I named it function hunchbaque_buddylisting($account, $buddies, $buddies_per_page)
  • 3. At this point your theme is rendering the buddylist page just like the buddylist module but you can now change your copy in your template.php to suit your needs - add user pictures, output as a list instead of a table, wrap each item in a div, whatever you need. Here a simple example of just adding one more column to the table for the user picutre.
  • <?php
    function hunchbaque_buddylisting($account, $buddies, $buddies_per_page) {
     
    drupal_set_title(t('%username\'s @buddylist', array('%username' => $account->name) + buddylist_translation()));

     
    // Add in another column for the user picture but with no text in the header
     
    $header = array('', t('@buddy', buddylist_translation()), t('online'));

      if (
    count($buddies)) {
        foreach (
    $buddies as $buddy) {
         
    // Load the user object for this buddy
         
    $account = user_load(array('uid' => $buddy['uid']));
         
    // Call theme('user_picture') to add the avatar to column 1
         
    $rows[] = array(theme('user_picture', $account), theme('username', $account), theme('buddylist_online', $buddy['online']));
        }
       
    $output .= theme('table', $header, $rows);
      }
      else {
       
    $output .= t(variable_get('buddylist_empty_text', buddylist_empty_text_default()), buddylist_translation());
      }

     
    $output .= theme('pager', NULL, $buddies_per_page);
     
      return
    $output;
    }
    ?>
AttachmentSize
buddylist_themed.png 52.52 KB

#17

ZrO-1 - October 15, 2007 - 15:38

OK guys, I hope this thread is still active. I've modified the function so that the user picture is appended to the user name without adding an extra column to the listing table. Basically since the buddylisting function is a theme function you can override it in your template.php file. What I did was copy the entire theme_buddylisting function and pasted it into my template.php file and changed the "theme_" to "phptemplate_". Now I could modify the function all I wanted without modifying the buddylist.module file.

Here is my version of the function (if you're going to copy/paste this be sure to exclude the <?php ?> part):

<?php
/**
* Theme override for the buddylist module buddylisting function
* This code is stock except that we add a user picture in front of the user name in the listing table.
*/
function phptemplate_buddylisting($account, $buddies, $buddies_per_page) {
 
drupal_set_title(t('%username\'s @buddylist', array('%username' => $account->name) + buddylist_translation()));

 
$header = array(t('@buddy', buddylist_translation()), t('online'));

  if (
count($buddies)) {
    foreach (
$buddies as $buddy) {
     
$rows[] = array(theme('user_picture', user_load(array('uid' => $buddy['uid']))).'&nbsp;'.theme('username', user_load(array('uid' => $buddy['uid']))), theme('buddylist_online', $buddy['online']));
    }
   
$output .= theme('table', $header, $rows);
  }
  else {
   
$output .= t(variable_get('buddylist_empty_text', buddylist_empty_text_default()), buddylist_translation());
  }

 
$output .= theme('pager', NULL, $buddies_per_page);
 
  return
$output;
}
?>

So as you can see, this just inserts the picture in front of the name without adding a whole extra column to the table.
hope that helps.

#18

ZrO-1 - October 15, 2007 - 15:40

LOL, it looks like dldege and I posted at about the same time.
Still though, his and mine are slightly different since my version doesn't add a new column. Other than that though either one should do the job.

#19

dldege - October 15, 2007 - 16:16

Either version is fine - the important thing is to understand the process as this is how (ideally) you customize a large part of Drupal - take a look at all the theme_ functions in buddylist.module and you can start to see all the things you can change with this technique.

#20

mariusooms - March 4, 2008 - 18:05

If anyone is interested for this functionality in buddylist2 and show buddies of the user you're currently viewing, check out this code you can import for views. http://drupal.org/node/229694

#21

MaineWebFX - April 3, 2008 - 22:28

Any ideas on adding the pictures to buddylist block?

#22

dangibas - April 6, 2008 - 05:39

The block and the display on the profile page are both controlled by...

function theme_buddylist($buddies) {
  return theme('user_list', $buddies);
}

This can be overridden in template.php using....

function phptemplate_buddylist($buddies) {
  //return theme('user_list', $buddies);
  return "X";
}

I'm still figuring out what to put in the override function though ;)

#23

SocialNicheGuru - May 13, 2008 - 01:19

subscribing...

I do have a question though.

when i use views, buddylist returns a usernode and my exposed filters are based on that usernode.

but i want to use filters and show fields of the usernode->uid that are stored in other node profile. how do i do that? Is views fusion helpful??

Chris

#24

drupalina - November 12, 2008 - 21:38

I tried both solutions of vrc3 (comment #14) and dldege #16, I think a vrc3 give a better solution because it also gives some profile details and importantly it gives us Buddy-actions, which make the surfing beteween individual buddylists a lot easier, and because it is more themable and more customizable. But it seems that the latest version of Buddylist-beta1 has some of these variables changed.

Could any of you coding gurus be kind enough to provide us with a theme template override to do display the following :
Avatar photo
Username
(preferably some profile fields so that we can change them accordingly)
Add to / Remove from my Buddylist
View that_username's Buddies
Send that_username a private message

(pretty much like Facebook was 1 year ago) -- I have seen MANY requests from Drupal users for this kind of listing of friends, but still there is no such code for us lesser mortals to use :(

Would some of you Drupal coders be kind enough to share this kind of code?

THANKS!

 
 

Drupal is a registered trademark of Dries Buytaert.