views:

434

answers:

3

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 available in the $user variable.

Many thanks

A: 

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!

WmasterJ
Hi WmasterJ, not entirely sure why you've been scored down for your response but I will give it try along with looking into googletorp's reply.Many thanks
Nick Lowman
Hi WmasterJ, your response worked well. I used your code to get the variable and I added to the $vars array in the phptemplate_preprocess_page. I'm still pretty new to Drupal so I'm not entiely sure what I'm doing. Thanks for your help
Nick Lowman
I think googletorp scored me down since he says in his answer that doing it the way i suggest would would overwrite the global user. Maybe you want to verify that since it works great for me but then again I havent verified what he says in his first sentence. Great that it helped you :)
WmasterJ
I downvoted you because your answer is both faulty and ineffective. Your code introduced a bug the OP didn't realize... Yet. Also I didn't say using your method would overwrite the global user. I explained the tradition using $user and $account.
googletorp
Could you explain the bug? And say how to avoid it, now im confused and everyone coming after wont know what is wrong, but the "Correct Answer" tick will still make them look at my answer,
WmasterJ
I moved the correct answer tick WmasterJ so others will know the right way even though yours did work
Nick Lowman
+1  A: 

$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.

googletorp
Hi googletorp, I'm still quite sure how I would get the variable out of the phptemplate_preprocess_user_profile and into another preprocess function. To be honest I'm not entirely sure how to create my own preprocess functions but I will look into your reponse and read up about how to do this. Also would you know why the response below from WmasterJ was scored down? Is it wrong to do things in Drupal?
Nick Lowman
Hi googletorp, I can understand what you mean now but I still have no clue how to acces the $account array. I tried looking it up but I couldn't find anything. I put dsm($account) into my phptemplate_preprocess_node and it came back with nothing.
Nick Lowman
$account is avaiable in the template not the preprocess function. Also it's not an array but an object.
googletorp
Thanks googletorp. Sorry to keep asking the questions.
Nick Lowman
+1  A: 

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

ashtonium
Thanks ashtonium. Maybe I'm wrong but there seems to be several ways to achieve the same thing with Drupal which is a bit confusing to a newbie but I'm enjoying it all the same.
Nick Lowman
ashtonium