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

leafish_paul - June 1, 2006 - 13:48

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!

#2

leafish_paul - June 1, 2006 - 14:13
Priority:critical» normal
Status:active» needs review

Like this.

AttachmentSize
roleweights_gethighest_returnsinglearrayelement.patch.txt 653 bytes

#3

leafish_paul - June 1, 2006 - 14:44

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?

AttachmentSize
roleweights_gethighest_returnsinglearrayelement_r1.patch.txt 872 bytes

#4

budda - June 4, 2006 - 23:41

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

budda - June 5, 2006 - 00:57

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

leafish_paul - June 5, 2006 - 17:32
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.

#7

leafish_paul - June 6, 2006 - 18:05
Assigned to:Anonymous» leafish_paul
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!

#8

Anonymous - June 20, 2006 - 18:15
Status:fixed» closed

#9

leafish_paul - June 28, 2006 - 12:54
Version:<none>» HEAD

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

 
 

Drupal is a registered trademark of Dries Buytaert.