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.

Comments

pfaocle’s picture

From the description I assumed the function would only return the single highest role id & role name/description.

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!

pfaocle’s picture

Priority: Critical » Normal
Status: Active » Needs review
StatusFileSize
new653 bytes

Like this.

pfaocle’s picture

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:

  $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:

  $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?

budda’s picture

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?

budda’s picture

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?

pfaocle’s picture

Status: Needs review » Needs work

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.

pfaocle’s picture

Assigned: Unassigned » pfaocle
Status: Needs work » Fixed

Fixed - applied to HEAD and 4.7 branch:

-      return array($rid, $roles[$rid]);
+      return array('rid' => $rid, 'name' => $roles[$rid]);

Cheers!

Anonymous’s picture

Status: Fixed » Closed (fixed)
pfaocle’s picture

Version: » master

budda: http://drupal.org/node/70772 -> another suggestion for the return value from role_weights_get_highest() from nedjo. Any thoughts?