By AvantSavant on
I would like to see users' information in a page node. I created some fields in a user profile like City and state, but I am unable to see them when I create a page and input PHP in the body text. I created a field called profile_city and I am trying to dereference it by entering
<?php
global $user
print t("$user->profile_city");
?>
This does not seem to work, however. I know that something similar works because I can this code to user_profile.tpl.php and it works just fine. What am I doing wrong?
Comments
Profile data not loaded by default
Profile data not loaded by default, need to call profile_load_profile() like this
ah, yes.
Thank you. That's one of those bits of information that I haven't come across yet. Do you know of any page that states this?
Well, actually
It might work better like this: (BTW, this does not require monkeying with theme code, this can be in any page.)
For documentation, try http://api.drupal.org/api/HEAD/function/user_load
Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database
NancyDru
Small correction to calling user_load()
When calling user_load() you need to pass an array, not just a uid, like this
Oops.
That's what I get for trying to hurry out the door.
Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database
NancyDru
question about the $global user line
It seems that I need to add
<?php global $user; ?>when I add my own php to a page I create within the Drupal web interface. However, I don't seem to need this line when I edit the php files directly, such as page-front.tpl.php or user_profile.tpl.php. I have not found an explanation for this. Does anyone know?
Yes, template files have certain variables you can access
If you look in the handbook at http://drupal.org/phptemplate, you can see links for some of the common tpl.php files which list variables you can expect to find set.
In the case of user_profile.tpl.php, the $user variable is not the global variable but a "local" variable that reflects the user information for the account being viewed.
Normally page.tpl,php files (such as page-front.tpl.php) do not normally have $user set so I wonder if it being declared early on in the file (it is not listed in the handbook and I can not find it being set by phpthemplate.engine which sets the default variables, it could be being set in the template.php for the theme you are using)