In the past there seemed to be a patch to add user relationship count to views options: http://drupal.org/node/216473

That was for version 5 and it seemed to have never been committed.

Is there already a way to get the relationship count in views? I'd like to be able to sort by relationship count (how many friends you have) and also post that number in a field. I can do the second if I make a custom views template and manually put in the count; but that's more complicated when it comes to sorting which is why it's needed in views.

Comments

ChrisRut’s picture

Category: support » feature

:subscribing:

YK85’s picture

subscribing

grzegorz.bartman’s picture

subscribing

manoz_79’s picture

subscribing

ajayg’s picture

These is an easy way (in any view) to get count of all records returned. So once you get your view working, and you should try the default relationship browser view as a model, just add following in the header or footer

<?php
    $view = views_get_current_view();
    print '<span class="view-counter"> Relationship count:  ' . $view->total_rows . '</span>';
?>

I typically keep the Title of views display blank and replace "Relationship count" text above with whatever title I want for display and add above to header.

YK85’s picture

Thanks for the help ajayg. That trick does indeed work for showing the relationship count for a view of one specific user.

Just to note - this does not work if you want a view with many users' username and the relationship count per user.
I believe this would require work to create a views field for this information.
-------------------------------
username | relationship count
username | relationship count
username | relationship count
username | relationship count
-------------------------------

Thanks!

ajayg’s picture

I have not tried myself. But have you looked at View group by module?

Offlein’s picture

This is useful functionality - and should be worked into a views handler because I'd eventually like to use it as an Exposed Filter. That is, users on our site can have a maximum of 3 buddies, and our search is basically an exposed view. It'd be great to give them the ability to see only users who had no buddy, had the maximum number of buddies, or otherwise.

And when I say "because I'd like it" I, of course, really mean "because since I'd like it, probably someone else would too". If there was a field handler for this already, presumably I (or someone) would be able to make a filter handler relatively quickly as well.

cerup’s picture

I agree with Offlein. This should really be made into a view handler. Searching/sorting/filtering people based on how many relationships they have is very common today and would probably be used by the majority of people using this module in some capacity I could imagine.

If I get a chance i'll see if I can create a working patch. I actually think the patch in my first post may still apply #216473: PATCH: Addition to views section -- User Relationship Count but needs to be reworked to add to the current dev version.

YK85’s picture

I will definitely do my best to help with testing if someone could work on a patch for views handlers. Thank you!

pedrofaria’s picture

This feature is not so easy to do...

The best way is do it by SQL but i guess that it must be very hard to do it with views schema... maybe i can get the count value when views render the field to output...

i will put the patch at #866400: Views filter patch for current user

cya

YK85’s picture

Thanks pedrofaria!
I look forward to testing it =)

pedrofaria’s picture

Field added to patch...

Please, review it... ;)

pedrofaria

robby.smith’s picture

It be very cool to have two fields.
User Relationships: Pending relationship counter
User Relationships: Approved relationship counter

Users | Pending count | Approved count
user1 | 0             | 5
user2 | 2             | 5
user3 | 1             | 7
user4 | 5             | 25
user5 | 10            | 3
YK85’s picture

There is some code for pending count at http://drupal.org/node/562138#comment-2008640

mmachina’s picture

is there way to adjust the code to read differently if user has only 1 friend... or more than 1 friend?

1 friend or
10 friends

YK85’s picture

I was wondering if anyone had any progress with this views relationship count?
Thanks!

diego.pasc’s picture

subscribing

mrf’s picture

Status: Active » Postponed

This would need #468442: Provide API function to return relationship counts completed before tackling views integraiton.