Is there a way to see how many friends a user has (ideally exposed to Views)? We'd like to build a block that shows the top users sorting descending by the number of friends, for example.

My apologies if it's possible and I have overlooked how. Otherwise I guess it's a feature request? :)

Thanks for your consideration.

-Daniel
Urbo

Comments

miljats’s picture

This works for me, try it.

Put this php code in a block header. Drupal´s core module PHP Filter must be enabled.

<div class="yourclass">
<?php
  $view = views_get_current_view();
  print t('Friends') . "  " . "<span class=\"normal\">" . "(" . $view->total_rows .")" . "</span>";
  ?>
</div>

Set input format to PHP code. You can style php code with your own css classes.

kerberos’s picture

Yeah, that gets the total number of a single user's friends on his friends page, but you can't do this for a list with multiple users or sort by the top 5.

I guess the only way the feature I am talking about would work with any decent performance is if Flag Friend also creates a table that stores each user's number of friends (the way Voting API stores the aggregate votes in addition to the single votes).

sirkitree’s picture

There is a bit of a start on this, a function called flag_friend_get_friend_count() which does a select count query for a given user id. It does some basic static caching, but flag_friend does not have a dedicated statistics table.

I'd be open to adding this if someone was willing to work on it.

sirkitree’s picture

Status: Active » Closed (works as designed)
mototribe’s picture

If you install the views_PHP module you can execute PHP code in a views field.
Add a "global text" field and paste this PHP code to display the number of friends for the logged in user:

global $user;
  $uid=$user->uid;
  $friend_count=flag_friend_get_friend_count($uid);
  if ($friend_count > 0) {
     print '<a href="user/' . $uid . '/friends">' . $friend_count . '</a>';
  }
  else { 
    print $friend_count; 
  }
    

If you want to show the number of friends for a user with the user ID in the URL you'll have to add the "uid" field to the view ABOVE your php field and use this code:

  $uid = $row->uid;
  $friend_count=0;
   $friend_count=flag_friend_get_friend_count($uid);
  if ($friend_count > 0) {
     print '<a href="/user/' . $uid . '/friends">' . $friend_count . '</a>';
  }
  else { 
    print $friend_count; 
  }
nielsvoo’s picture

Oke, this works fine, i put the code in a custom token so it can be used everywhere on my site.

Is there also a way to output the amount of pending and flagged friendships for a user using Drupal 7?

Thanks
Niels

nielsvoo’s picture

Is there a way to combine this code with this sql statement, otherwise blocked users won't ignored.

$sql = $this->query("SELECT * FROM `users` WHERE `status` != 0");

Thanks
Nielsvoo

socketwench’s picture

I'd probably just query the DB directly for both. It's not best practice, but the 6.x branch is pretty stable:

SELECT count(ff.friend_uid) FROM `flag_friend` as ff, `users` as u WHERE u.uid = ff.uid AND u.status != 0 AND u.uid = $current_user_id