role_weights_get_highest returns two values
budda - June 1, 2006 - 01:01
| Project: | Role Weights |
| Version: | HEAD |
| Component: | Code |
| Category: | bug report |
| Priority: | normal |
| Assigned: | leafish_paul |
| Status: | closed |
Description
When feeding my user's $user->roles array (array(1) { [2]=> string(18) "authenticated user" }) into $role = module_invoke('role', 'weights_get_highest', $user->roles); it's returning two elements in the array:-
array(2) { [0]=> int(2) [1]=> string(18) "authenticated user" }
From the description I assumed the function would only return the single highest role id & role name/description. Currently this kinda breaks the use of this module I think?
Any idea what the [0] => 2 is? I tried both the current CVS and 4.7.0 versions of the module. Both gave the same response.

#1
It does: the first element of the array is the 'highest' role id (2) and the second the 'highest' role name. There may have been a reason for returning it like this, or perhaps not... I'll take a look. Ideally, I suppose what we want is:
array (2 => 'authenticated user')returned. Cheers!
#2
Like this.
#3
Lets return an empty array too, if no matching role is found.
The only place I've currently been calling this is in a theme's template.php file, when theming usernames:
<?php$highest_role = role_weights_get_highest($auser->roles);
return _get_hat($highest_role[1]) . l($auser->name, 'user/'. $auser->uid);
?>
which with this patch now becomes:
<?php$highest_role = role_weights_get_highest($auser->roles);
return _get_hat(array_shift($highest_role)) . l($auser->name, 'user/'. $auser->uid);
?>
using array_shift to obtain the first array elements value. Is there a similar method to obtain the first key when the role id is required and not the role name? Is this a preferable return value for you, budda?
#4
Ahh, so that's what the first element is for. A bit obscure format :)
array (2 => 'authenticated user')is just like I was expecting. Woo. Thanks! Is this in CVS?#5
Hmm, i see what you mean by the array_shift() stuff for the key.
Maybe an indexed array would work better, like:
return array('rid' => $rid, 'name' => $roles[$rid]);Or return the result as an stdclass object?
#6
No, I've not applied the patch yet, mainly for the finickity concerns about the return format. I think an associative array is the way forward - will take a look possibly tomorrow. I'm in the middle of moving house at the moment: hectic-tastic.
Thanks for the feedback.
#7
Fixed - applied to HEAD and 4.7 branch:
- return array($rid, $roles[$rid]);+ return array('rid' => $rid, 'name' => $roles[$rid]);
Cheers!
#8
#9
budda: http://drupal.org/node/70772 -> another suggestion for the return value from role_weights_get_highest() from nedjo. Any thoughts?