Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Having written a fair bit of code using the User Relationships API, I recommend making the following changes to the API:
- The array returned by
user_relationships_relationship_types_load()
should be indexed by rtid, instead of just an arbitrary index. That way, if you happen to have a relationship object, you can easily gather additional information about it's type. - A new function,
user_relationships_load_relationships($criteria)
, should be added. This function would behave like the recently addeduser_relationships_count_relationships($criteria)
function, except that instead of returning the count of relationships that match the criteria, it returns an array of relationship objects that match. [See http://drupal.org/node/183011 .] If 'uid' is specified in the criteria, the array would be indexed by $relatee, where $relatee is relative to the uid specified in the criteria; otherwise, it would have an arbitrary (numeric) index. That way, you could do the following:
foreach (user_relationships_load_relationships(array ('uid' => $user_id)) as $relatee_id => $relationship) { $relatee = user_load(array('uid' => $relatee_id)); ... }
This would save folks from having to do
$relatee_id = ($relationship->requester_id == $user_id) ? $relationship->requestee_id : $relationship->requester_id;
virtually everywhere they load relationships. - The
user_relationships_load_all_for_user($uid)
could be deprecated, as it's equivalent touser_relationships_load_relationships(array('uid' => $value))
. - The
user_relationships_relationship_load($rid)
could also be deprecated, as it's equivalent touser_relationships_load_relationships(array('rid' => $value))
.
What do folks think? Does this sound reasonable?
Comment | File | Size | Author |
---|---|---|---|
#3 | urapi.patch | 4.47 KB | prfctns6@gmail.com |
Comments
Comment #1
prfctns6@gmail.com CreditAttribution: prfctns6@gmail.com commentedI really want
user_relationships_load_relationships($criteria)
to return an array indexed by relatee whenever possible. But it seems a little weird to have that behavior only happen if $criteria contains a key of 'uid' — it seems like the behavior is hidden from the API user. Another option is to have it accept an optional second parameter, $relater. If $relater is set, use that to determine the $relatee to index by. My problem with that is that it's possible to specify the criteria such that some of the matching relationships don't have $relater as either the requester_id or the requestee_id. The returned array might then be unpredictably indexed, possibly leading to weird, hard-to-track-down bugs.What do folks think? How should the function determine the uid of the relater: by inspecting the criteria, which is a bit cryptic but always safe, or by accepting a parameter, which is more explicit but riskier?
Comment #2
prfctns6@gmail.com CreditAttribution: prfctns6@gmail.com commentedWell, I'll just have a big ol' conversation with myself here. :-)
I realized that there can be more than one relationship between 2 individuals, so returning a flat array indexed by relatee would only return one of their relationships, selected somewhat arbitrarily. That's obviously unacceptable, so now I'm thinking of having 3 API functions:
ur_count_relationships() just returns a count, as it does already. ur_load_relationships() returns a flat array with an arbitrary index. ur_load_relationships_by_relatee() returns an array indexed by $relatee relative to $relater. Each element of the array is itself an array of the relationships that exist between $relater and $relatee. All relationships that match the criteria but don't involve $relater are returned in the array indexed by $relatee=-1, so as not to conflict with any valid user id.
I think this works ok., and is fairly comprehensible. Anybody else want to chime in on this conversation? :-)
Comment #3
prfctns6@gmail.com CreditAttribution: prfctns6@gmail.com commentedHere's a patch that implements the API as described in update #2 above.
Comment #4
prfctns6@gmail.com CreditAttribution: prfctns6@gmail.com commentedIn the absence of any objections, I've committed the changes to the API.
I also updated the criteria for counting/loading relationships such that the values of uid, uid1, uid2, rid, requester_id, requestee_id and rtid can be scalars or arrays; if they're arrays, the functions match relationships where the appropriate column matches any element in the array.
Comment #5
(not verified) CreditAttribution: commentedAutomatically closed -- issue fixed for two weeks with no activity.