At the moment, I think the buddylist interface is a little cumbersome. To add a buddy, a user must first navigate to a user's profile page. This is not obvious to causal users. I think a better method would be to replace all usernames on a site with username (add to buddylist) or username (remove from buddylist). This gives an immediate way to add any user to your buddylist. Quick, easy, intuitive.

This functionality can be implemented by having the module override the phptemplate_username function.

Pros:

  • Easy for users to immediately add people to a buddylist
  • Easy for developers, the logic behind add/removing buddylist links can be handled in one place
  • Easy to include in the module, no need for extra template files
  • Less obtrusive than a block, and can apply to multi-node pages - a block would be useless on a multi-node page

Cons:

  • Overriding the phptemplate function is total hack of theme system
  • Themes might have problems really overriding the look of usernames
  • Inefficient: scanning the buddylist multiple times for everyuser could be CPU intensive, but I think it could easily be an opportunity for some optimization and caching

To test this patch, just drop the following code into your buddylist file (I think it will only work in HEAD):

<?php
function phptemplate_username($object) {
  global $user;
  
  /* Use the default theme_username for anonymous users, nodes by this user */
  if ($user->uid == 0 || $object->uid == $user->uid || $object->uid == 0) {
    return theme_username($object);
  }
  
  /* an array, keyed on buddy uids */
  $buddies = buddylist_get_buddies($user->uid);
  
  /* Find out if this buddy is in the user's buddy list */
  foreach ($buddies as $buddyuid => $buddystructure) {
    if ($buddyuid == $object->uid) {
      $output .= theme_username($object);
      $output .= " (";
      $output .= l(t('remove from buddylist'), 'buddy/delete/' . $object->uid);
      $output .= ")";
      
      return $output;
    }
  }
  
  /* The user is not in the buddylist, give a link to add */
  $output .= theme_username($object);
  $output .= " (";
  $output .= l(t('add to buddylist'), 'buddy/add/' . $object->uid);
  $output .= ")";
      
  return $output;
}
?>

Comments

robertdouglass’s picture

Title: Significant redesign of the buddylist interface » Theme username to include add/remove from buddylist links

Your code overlooks the role based permissions needed. Here is an updated version that depends on the buddylist module being prepared for 4.7. Note that it includes use of theme functions that aren't committed as of this writing, but will be soon.

function phptemplate_username($object) {
  global $user;
  /* Use the default theme_username for anonymous users, nodes by this user */
  if ($user->uid == 0 || $object->uid == $user->uid || $object->uid == 0) {
    return theme_username($object);
  }
  if (!user_access('maintain buddy list')) {
  	return theme_username($object);
  }
  
  /* an array, keyed on buddy uids */
  $buddies = buddylist_get_buddies($user->uid);
  /* Find out if this buddy is in the user's buddy list */
  foreach ($buddies as $buddyuid => $buddystructure) {
    if ($buddyuid == $object->uid) {
      $output .= theme_username($object);
      $output .= " (";
      $output .= theme('remove_from_buddylist_link', $object);
      $output .= ")";
      return $output;
    }
  }
  /* The user is not in the buddylist, give a link to add */
  $output .= theme_username($object);
  $output .= " (";
  $output .= theme('add_to_buddylist_link', $object);
  $output .= ")";
  return $output;
}

Changed title to be more specific

robertdouglass’s picture

Status: Needs review » Fixed

Other notes: this is for use with a PHPTemplate based theme. If you are using a different theme engine, you need to rename the function to use the name of your theme.

For PHPTemplate themes, the best place for this and other similar functions, is in a template.php file in your theme's directory. If you have such a file, paste this funciton into it. If you don't have one, create it.

I'm including this code in the INSTALL.txt file as that is the best place for sharing such theme functions until we actually have a theme functions repository. Marking as fixed.

mfredrickson’s picture

Good idea for putting it in the readme. Though I think a better idea might be to create a contrib module that does two things:

1. Implements this function for all theme engines.
2. Implements hook_user('load') to add buddies for the current user object. (Unless the the buddylist_get_buddies caches the look up..., in that case this if fine). Looking up the current user's buddies each time Drupal encoungers a username could be a potentially expensive operation....

I'll add this module to my lengthy to do list.

Keep the issue "fixed" anyway.

robertdouglass’s picture

caching the lookup is good. I'll do that right now. You can work on the idea to make the theme function generally available.

robertdouglass’s picture

It was already cached. No worries.

Anonymous’s picture

Status: Fixed » Closed (fixed)