Community & Support

Accessing profile fields outside of the profile node

Hi everybody-

I've been working pretty hard the last few weeks to get a new site up for my company but just hit a problem that I'm having a heck of a time trying to solve. These forums have been great in helping me with past issues, but I can't seem to find a solution this time around.

I'm trying to get a custom profile field to display in the header on all pages:

"Welcome [First Name] | Manage My Account | Logout"

I was doing this hand in hand with pulling custom profile fields into forum posts (profile_firstname, profile_lastname, profile_company, profile_city, profile_state) - which I got all to work thanks to the Real Name thread.

Unfortunately, what worked there (<?php print $profile_firstname ?>) does not work in the header.

Currently, I have the following code in the header that works great, but just shows the username (courtesy of Custom login form examples):

<?php global $user; ?>
<?php if($user->uid) : ?>

<div id="account">

Welcome <?php print ($user->name); ?> | <?php print l(t('Manage my account'), 'user/'.$user->uid); ?> | <?php print l(t('Logout'), 'logout'); ?>

</div>

<?php endif; ?>

I've tried replacing <?php print ($user->name); ?> with the following, but nothing works:

<?php print $profile_firstname ?>
<?php print ($user->profile_firstname); ?>
<?php print ($profile->profile_firstname); ?>
<?php print ($profile->firstname); ?>
<?php print $firstname ?>

As a caveat, I'm a total PHP neophyte (if you couldn't tell already).

Anybody have any ideas?

Thanks much in advance!
sfobrian

Comments

...

The following code works:

<?php
global $user;

print
$user->name;
?>

That's because 'name' is a basic property which is provided by Drupal core.

Now, let suppose you created a 'firstname' field (it's actually 'profile_firstname') using the built-in profile module. The following code, however, won't work:

<?php
global $user;

print
$user->profile_firstname;
?>

That's because non-core properties aren't loaded automatically. This is for performance reasons. You have to load the user object explicitly:

<?php
$uid
= $GLOBALS['user']->uid;
$user = user_load(array('uid' => $uid));

print
$user->profile_firstname;
?>

(having said that, $user->profile_firstname might actually work without doing user_load() first. That happens when some other module, e.g. Organic Groups, loads this object.)

--
Help save BLOCKQUOTE

Thanks Mooffie - I can see

Thanks Mooffie - I can see how that makes sense (and why my initial attempts were off). I'm off to try your suggestion right now!

Quick follow-up: This

Quick follow-up: This totally worked! Thanks so much Mooffie! Totally killed two birds with one stone too - I ran into some trouble with the Real Names hack for the forum (it worked on topic posts, but not on the comments). A little massaging of your code got that straightened out!

Awesome! Thanks for the Christmas present! :-)
Brian

Excellent

Thanks! This helped me with a similar issue I was having.

Thanks Mooffie! Refreshing to

Thanks Mooffie!

Refreshing to get a simple, working solution after a day of headache!

nobody click here