开发者

Accessing user profile variables

开发者 https://www.devze.com 2022-12-20 06:33 出处:网络
Using the profile module I\'ve created a textfield called profile_real_name which the user fills out when registering. How do I access this variable in the node.tpl.php?

Using the profile module I've created a textfield called profile_real_name which the user fills out when registering. How do I access this variable in the node.tpl.php?

I used the dsm($user) function to output the user variables and it contained everything except the data for the profile_real_name

I also ran dsm($vars) on the phptemplate_preprocess_user_profile and I could see it contained an object called account which did contain the info I needed but this object isn't availabl开发者_Go百科e in the $user variable.

Many thanks


If you want to access the author's profile information in node.tpl.php, then you want to work with a phptemplate_preprocess_node function rather than the user_profile one. The node preprocess function doesn't have an $account object by default though, so you'll have to load it in:

This goes in the phptemplate_preprocess_node function in your template.php file:

if ($vars['uid']) {
  $vars['account'] = user_load(array('uid' => $vars['uid']));
}

Then you would be able to access the author's profile values in your node.tpl.php. The value you asked about specifically would be:

print($account->profile_real_name);

However, it sounds like you might want the node author's name to appear as the profile_real_name value rather than their account name?

If so, a MUCH more efficient way would be to override the theme_username function.

That's not directly what you asked about so I won't go into it here, but this post on the drupal.org forums would be an excellent place to start for Drupal 5 or 6: http://drupal.org/node/122303#comment-204277


$account is what you usually call a user that isn't the global user to avoid accidently overwriting the global user which would result in the user be get logged in as that user.

I just did a bit of checking and the easiest way to solve your problem is to use $account in the template instead of $user.

Using $user in the template or doing like WmasterJ suggests is faulty. You will post the wrong data. You will post the data of the logged in user not the data of the user who's profile is being watched. This bug will happen when you view all other users' profile than your own.

Preprocess functions is not hard to make, in your template.php file in your theme you just replace phptemplate with your theme's name defined the code. In this case you wont need to alter the preprocess function, since you already have what you need.


If you want to do this within for instance the user-profile.tpl.php all the information you need exists within the $account array.

Otherwise you can access user data by loading a user object based on it's id (of the currently logged in person that is, or if you can query the DB and get uid that way).

First get the uid of the current user:

$uid = $user->uid;

Then load the a user object:

// Create user objets based on uid ()
$user_obj = user_load($user->uid);

Then load that users profile variables:

// Load profile
profile_load_profile($user_obj);

Now the $user_obj variable (which is passed by reference to profile_load_profile) has an object with the the profile information that can be accessed like this:

$user_obj->profile_real_name

Hope it helps!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号