I needed this functionality for my own site, and I thought I would contribute it back to the community.
I've attached a small patch that adds a view field with a count of the number of relationships for a particular user.
This module can be used for a variety of situations -- to count the number of employees under a manager, or protégés under a mentor, or just to see how connected a particular user is in the social fabric of a community.
It works well for those using the usernode module, and with a slight tweak can accommodate other profile solutions.
I'm just learning and this is my first code contribution, so please take a hard look and see if anything in the patch could be done better.
| Comment | File | Size | Author |
|---|---|---|---|
| #2 | user_relationship_views.patch | 1.36 KB | sprsquish |
| user_relationship_views.patch | 2.7 KB | neopoet |
Comments
Comment #1
sprsquish commented@alakon
Hey, thanks for the contribution. I'm going to work on integrating it with the module, but I wanted to comment on your code a little. I hope you wont mind some constructive criticism.
Here's your main function:
So the first issue is that you have three separate "if" statements in there. We already know that the value of
$field_data['options']is going to be one of three different values, but never a combination. Right now each condition is checked creating extra work that doesn't need to be done. So those three statements should be turned into if/else if/else:Now the max number of comparisons have been cut to two. Also, if the first condition evaluates to TRUE none of the others run. This may be small, but it cuts out unnecessary steps.
In the same code, none of your conditional tests are actual conditions. "=" is an assignment operator, what you're looking for is actually "==" to do the comparisons.
Those are my basic PHP criticisms. When I finish the patch, I'll post it and show you what I did and why.
Thanks again for the contribution.
Comment #2
sprsquish commentedOkay, here's my version.
Here's the relevant parts of the patch:
Here I've set the values of the select to be param names that will later be passed to
user_relationships_load. Down in the code, I've loaded the node in order to reach the UID it belongs to. Then usinguser_relationships_loadwe grab the count.Comment #3
neopoet commentedAwesome! Thanks for improving the code.
I'm excited to make my first modest contribution to Drupal.
Comment #4
sprsquish commented