Hey Folks,

I've been searching all around, but I can't figure out how to simple print specific field from a user's profile on a node they have created.

I would like to print user first name, last name, and some other custom fields I have created in the profile module above or in the body of the node (whichever is easier).

I don't have fancy needs so I don't want to muck around with all the profile-as-nodes modules.

From $nid, how to I get the $uid of the person who submitted it, and then, how do I print the fields I want?

Thanks for your help, I'm new to PHP.

Peace
jmjohn

Comments

WorldFallz’s picture

The author of a node is "$node->uid". To load the profile of a that user and access the fields you should be able to do something like (untested):

<?php
profile_load_profile($user);
print($user->profile_[fieldname]);
?>

===
"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." -- Benjamin Franklin
"Search is your best friend." -- Worldfallz

glass.dimly’s picture

WordFallz,

Thanks much for your quick response and your help. Much appreciated.

I still have a few questions.

I tried this:

profile_load_profile($node->uid);
print($node->uid->profile_first_name);

and this:

profile_load_profile($node->uid);
print($node->uid->profile_first_name);

Neither of which worked. I am pretty new to PHP, coming from Java, so I have some fundamental confusions.

So I get hold of the $user first which I would save to a local variable. The question is, is $user an object, an array, or just a number like uid? If uid is a number, how can I use it to get hold of the $user object? So, I get $user and I save it to a local variable that matches its type. Okay.

I feed the $user to profile_load_profile. However, the docs say that it requires a variable &$user. Whats the difference between &$user and $user?

This returns... ...what? An array of strings? A specific field that is a string? I can't tell from the docs. How would I print it's output?

Or...
Can I just use -> to grab the profile fields from the $user once I have hold of that object? Great, that's easy, But, again, how do I get hold of $user from uid and what type of thing is it?

Thanks much,
jmjohn

WorldFallz’s picture

I'm an idiot-- I don't know what pipe I was smoking from when I posted my reply above, lol.

All you need to do is:

<?php 
$author = user_load(array('uid'=>$node->uid));
print $author->profile_[fieldname];
?>

You may want to do a quick "print_r ($variables)" to verify that your theme doesn't already use $author first. If it does, depending on what $author represents, you can either use it or use something else in the code above.

For my original answer to work, it would have needed "global $user" as the first statement, but that still wouldn't have been what you wanted-- it would have loaded the profile of the currently logged in user, not the author.

You surmised correctly-- $user is an object representing the currently logged in user (with global $user). But you want the author, which is $node->uid (but that's not an object, just a integer). To load a user object for the author you need the user_load statement above. user_load also loads the profile, so the profile_load_profile (which is looking for the $user object, not simply a uid which is why $node->uid won't work there) becomes unnecessary.

The difference between &$user and $user is that pre-pending the '&' passes the variable by reference.

Hope that helps... post back if you want more info and sorry for sending you down a spurious path....

===
"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." -- Benjamin Franklin
"Search is your best friend." -- Worldfallz

glass.dimly’s picture

Thanks Wordfallz!

That worked for me!

And have no fear, the path you sent me on was less spurious than the path I was already on.

$user must be an array, because you have assigned $author to be an array equal to the return of load_user, which means it is not an object.

I can now easily generate the information that I want.

Thanks,
jmjohn

WorldFallz’s picture

Excellent-- glad it worked.

...$user must be an array...

being a java guy you probably know more about it than I do (i'm only an html/css jockey barely learning php), but the api page for user_load says the return value is "A fully-loaded $user object..." and when I do print_r on $author it says "stdClass Object". I've heard that php's support of oop is spotty at best, so maybe it's a php version of an object.

===
"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." -- Benjamin Franklin
"Search is your best friend." -- Worldfallz

safetypin’s picture

I'm working with a CCK content type with a user-reference field. I'm also using node_profile to let users create a public profile that's separate from the drupal user profile. What I'd like is to have that list of nodes generated by the user reference "reverse link" option on the node_profile nodes. I figured if the data is loaded on the profile page, and I can load the user profile data inside the node, I ought to be able to get at it.

Is there a way to have drupal output the entire data structure of the $author object (as loaded in the code listed here) so I can figure out where the node references are?

WorldFallz’s picture

sure is...

<?php
print '<pre>';
print_r ($user);
print '</pre>';
?>

===
"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." -- Benjamin Franklin
"Search is your best friend." -- Worldfallz

doublejosh’s picture

Thanks for the reminder on load_user() parameters. It's easy to forget it needs an array.

naero’s picture

For people like me who are a bit clueless (like me) ... if your custom profile fieldname is "profile_firstname" ... the code should look like this:

<?php
$author = user_load(array('uid'=>$node->uid));
print $author->profile_firstname;
?>

I literally stared at the code and tried different variations for about 45 minutes. Grrr...

Point is, in WorldFallz's code ... the "[fieldname]" should be replaced with what you have set in your custom profile field name. It's a bit obvious for some, but maybe not for others like me!

bmblack’s picture

Your post helped - I was definitely putting my field name within the brackets: profile_[city].

I changed the code to match yours and instead of firstname, i entered city. So my field is profile_city.

My problem though is that it's not returning anything. When I view the page, the block only shows the title that I entered for it. I made sure to have php enabled and I tried other php code snippets that I found on drupal.org and they worked fine... any thoughts?

radiofranky2009’s picture

I tried this code inside of a block, but it seems like lost in translation of the $node id vs uid.

this is what coming out from the block. The code works great if you put in page.tpl.php

stdClass Object ( [uid] => 0 [name] => [pass] => [mail] => [mode] => 0 [sort] => 0 [threshold] => 0 [theme] => [signature] => [signature_format] => 0 [created] => 0 [access] => 0 [login] => 0 [status] => 0 [timezone] => [language] => [picture] => [init] => [data] => [timezone_name] => [roles] => Array ( [1] => anonymous user ) )

WorldFallz’s picture

blocks don't have a $node-- you need to do a node_load to get it._

radiofranky2009’s picture

Thanks for your help, I tried this but nothign changed. It still can't get the user profile info from the node.

$uprofile = user_load(array('uid'=>$node_load->uid));
print $uprofile->profile_logo;
print_r ($uprofile);

WorldFallz’s picture

radiofranky2009’s picture

:) I'm not a coder, just trying to build something with drupal.

so, how should I get author id vs respect to the node? and get all the CCK profile field that user registered with?

thanks

WorldFallz’s picture

If the block will be on the page displaying the node in question, you can try something like:

$author = user_load(array('uid'=>node_load(arg(1))->uid));
print $author->profile_firstname;
radiofranky2009’s picture

update: i got it working! thanks

one more question, if the profile is created with cck profile, how do I create a block to retrieve those fields?

thanks

radiofranky2009’s picture

I tried your code and printed all the variables. I've create a block to display it on the right column of my node.

code used

$author = user_load(array('uid'=>$node->uid));
print_r ($author);

this is what returned.

stdClass Object ( [uid] => 0 [name] => [pass] => [mail] => [mode] => 0 [sort] => 0 [threshold] => 0 [theme] => [signature] => [signature_format] => 0 [created] => 0 [access] => 0 [login] => 0 [status] => 0 [timezone] => [language] => [picture] => [init] => [data] => [timezone_name] => [roles] => Array ( [1] => anonymous user ) )

drupalfan81’s picture

Great post WorldFallz! Thank you so much for the advice you have given here as it came in real handy. I have one follow up question though.

This works, but for some reason when I print the profile field of my user, it's removing the HTML tags. It prints the text as if it was plaintext. For example, I am outputting the profile_about field which my user has 2 paragraphs of text. There are

tags in there and it displays fine on his user profile page. But when I output this value in the contemplate for the node the user added, it just spits it out as one long paragraph with no line breaks. Is there a way to get it to output without stripping the HTML tags?

WorldFallz’s picture

I don't believe that's default behavior. Can you post the exact code you're using to load the user and output the variable(s)?

drupalfan81’s picture

Hi WorldFallz,

Okay, so I looked at the row in the database to see how it was stored and it was stored without the

and
tags. Basically the about field on the user profile page is a standard text area field added by Drupal's built in profile fields. When the user edits their profile they can put in the about us section content. It's just a plain text box with no ckeditor or anything. The user can input something like:

"blah blah blah blah. blah blah blah

blah blah blah."

When they save it, it will save that line space with it. So when they view the about me section on their profile page or in the edit box it appears with the space between those two sentences. However, there are no
tags in there. So basically when PHP outputs it in the method you described, blah blah blah just ends up being continuous text without the space. So it seems like this is what would happen, is there a way to preserve the space on output using php. As for my code, it looks like this:

$yoauthor = user_load(array('uid'=>$node->uid));

echo '<div style="overflow: hidden;">';
echo '<div style="float:left; margin:10px;">';
echo '<center>';
echo '<img src="' . $usrimage . '" width="150" style="margin: 5px 5px"/><br />';
echo '<a href="' . $usrpage . '">' . $usrname . '</a>';
echo '</center>';
echo '</div>';
echo '<div style="margin:10px;">';
echo '<p>';
if ($yoauthor->profile_aboutme) {
echo '<b>About Me:</b><br /><br />';
print $yoauthor->profile_aboutme;
echo '<br /><br />';
}
WorldFallz’s picture

The text format normally governs what is shown on output-- there's a filter that preserves line breaks on output. However, I'm not familiar enough with the internals of user_load to know if that text format is run automatically. You might to have to do it manually somehow.

drupalfan81’s picture

thanks Worldfallz. Well I somewhat solved the problem for now. I just enabled ckeditor onto this text area field so that it will automatically create the
and

tags so that when the contemplate outputs the field, it will have the spaces the user thinks it will add. If anyone comes across this and knows how to maintain the line breaks without using a wysiwyg editor, let me know. Thanks!!

glass.dimly’s picture

http://stephenondrupal.blogspot.com/2007/10/displaying-full-name-of-auth...

Edit: this link doesn't provide working code. Go with the above.

designbump’s picture

I was searching for a bit on the proper way to do this before I stumbled upon this post. Just wanted to say thanks!

Best,

John

http://designbump.com

andrenoronha’s picture

hey maybe someone can help me

i use content profile with taxonomy terms in the profile nodes.
i want to create a view to show users with same interests as the loged user. (same taxonomy tags)
i have a views like this but with common nodes.
how could i get the tags and pass them as arguments in the view?

glass.dimly’s picture

Andre,

Respecfuly, I'd be glad to help you, but I don't the answer to your question. You'll get more traffic for your post if you create a new forum post in the appropriate category rather than responding to an old (and unrelated) post.

Peace,
Jeremy

nsuit’s picture

I had to add [LANGUAGE_NONE][0]['value'] to the solution above in my module:

<?php
$author = user_load(array('uid'=>$node->uid));
print $author->profile_firstname[LANGUAGE_NONE][0]['value'];
?>
arnoldbird’s picture

global $user;
$profile = profile2_load_by_user($user, 'main');
$ffn = field_get_items('profile2', $profile, 'field_first_name');
print $ffn[0]['value'];