views:

300

answers:

3

Hi all,

This statement gives me the value I want in Drupal (namely the last user role):

global $user; return (end($user->roles));

however I need to convert it to this format:

return array(
  0 => array('value' => value for value),
  // You'll usually want to stop here. Provide more values
  // if you want your 'default value' to be multi-valued:
  1 => array('value' => value for value),
  2 => ...
);

and I've got no idea how to (also, is this format a multidimensional array?)...

Any ideas? Thanks for any help, Theo.

A: 

I'm not sure what you are aiming for exactly. For the record the form of $user->roles is

array(
  rid => 'role_name',
  ...
)

Using the statement above, you actually don't get the late user role, but the user role with the highest id.

I'm guessing you want to get a format like this,you're not to clear about this.

array(
  0 => array(rid => 'role_name'),
  1 => array(rid => 'role_name'),
  ...
)

if that is the case you can get it with a simple foreach loop:

$result = array();
foreach ($user->roles as $key => $value) {
  $result[] = array($key => $value);
}
return $result;
googletorp
hmm, really I'm just trying to get the value "Line Manager" which "return (end($user->roles))" does, but which is incompatible with the format required - I need to insert "Line Manager" as the value of the field. But obviously Line Manager will be replaced with "Employee" if it is an employee, etc. I don't know how else to explain this though, as that's the format which is specified and if I try to do anything else I get errors showing up.
theo
This: "global $user; return (end($user->roles));"Gives me this error message:"The default value PHP code returned an incorrect value.Expected format:return array( 0 => array('value' => value for value), // You'll usually want to stop here. Provide more values // if you want your 'default value' to be multi-valued: 1 => array('value' => value for value), 2 => ...);Returned value: Line Manager"
theo
A: 

Why not simply:

global $user;
print_r($user->roles);

That will show you a users roles. I'm not sure why you'd want the last one. It already returns in the format you are asking for. From there its up to you to extract the role you are looking for.

To move them into a keyed array:

global $user;

$result = array();
foreach ($user->roles as $key => $value) {
  $result[] = array($key => $value);
}
return $result;
Kevin
Sorry, I should have clarified this. I need it in this format as this is the format which the Content Taxonomy requires as the default value. I also need to automate this - i.e. I don't want to have to specify which role to use, so either stating end or the position which that role will end up in is my solution - the key is automatically assigned by Drupal based upon the ID, but I know that the role I want will always be in that position.
theo
Ok I edited my post.
Kevin
A: 

Thanks for the help - you've made me understand this more now. It turns out that it wasn't working because you have to use an ID number and not a name. So the final code was:

global $user; return array( 0 => array('value' => end($user->roles)) );

So I guess that you are having to build a multidimensional array is it? for this to be in the correct format...

theo