Allowed values - user info

gregolin - February 6, 2009 - 18:28

Hi.
In "Allowed values" -> "PHP code", how can I use user info?
For example:

==============

$query = "SELECT rid FROM users_roles WHERE uid = ".$user->uid;
$result = db_query($query);
while ($row = db_fetch_array($result)) {
$rid[] = $row['rid'];
}
return $rid;

==============

My problem is that $user->uid is empty...
Thanks.

_

WorldFallz - February 6, 2009 - 18:30

If you want the uid of the currently logged in user, you need to use global $user; first.

===
"Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime." - Lao Tzu
"God helps those who help themselves." - Ben Franklin
"Search is your best friend." - Worldfallz

Ow, yes... soso ok... My

gregolin - February 6, 2009 - 18:37

Ow, yes... soso ok...
My problem now is with the query... Do I need to put some code before query? I think that I don't have open connect to it.
Thanks.

db_query uses the active

nevets - February 6, 2009 - 19:02

db_query uses the active connection, there is no need to open one.

Also, a more Drupal approach to

$query = "SELECT rid FROM users_roles WHERE uid = ".$user->uid;
$result = db_query($query);

would be
$query = "SELECT rid FROM users_roles WHERE uid = %d";
$result = db_query($query, $user->uid);

Note you really do not need the query as $user->roles already contains the information. If you are using this for a CCK, type integer with a select widget you can simplify the PHP to

global $user;
return $user->roles;

I have not tried it but there is also the Role Reference module for CCK.

Ok... with this I see my

gregolin - February 6, 2009 - 19:08

Ok... with this I see my authorized roles... but I need the content types that I have permission to access (to edit)... how can I return this?

If $user is suppose to refer

nevets - February 6, 2009 - 18:32

If $user is suppose to refer to the current user you need a global $user; otherwise $user is undefined.

 
 

Drupal is a registered trademark of Dries Buytaert.