0

I have a integer field connected to my users that I would like to print out in the header of my site. I would like to display the value of the field for the user that is currently logged in. When I do print dpm($user) it shows me information about the currently logged in user, but not any of the custom profile fields associated with them.

2
  • How are you connecting your custom field to the user, What module are you using and what Drupal version are you running? Commented Dec 15, 2011 at 9:05
  • Sorry I wasn't more clear. I am running Drupal 7, and just have a integer field on my users. No module, just a core field.
    – oobie11
    Commented Dec 15, 2011 at 22:41

3 Answers 3

2

First I want to thank @mototribe and @Kimi for helping me figure this out. I was able to get access to my field by putting

<?php 
global $user;
$user_fields = user_load($user->uid);
$MYVARIABLE = $user_fields->field_MYFIELD;
?>

at the start of my page.tpl. Then I had to print it out by adding this to my page <?php print $MYVARIABLE['und'][0]['value'];?>

1

Add a THEME_process_page function to your template.php (replace THEME with the name of your theme):

global $user;
$user_fields = user_load($user->uid);
$variables['nickname'] = '';
$nickname = $user->field_nickname;
if($nickname) {
  $variables['nickname'] = $nickname['und'][0]['safe_value'];
}

Now you can add this to your page.tpl.php:

<?php print $nickname ?>
3
  • When I try and use this method, I get errors
    – oobie11
    Commented Dec 16, 2011 at 1:55
  • I am assuming that by $nickname you mean my field?
    – oobie11
    Commented Dec 16, 2011 at 2:05
  • yes, that's correct
    – uwe
    Commented Dec 16, 2011 at 16:42
1

I have a integer field connected to my users that I would like to print

This is somewhat confusing, have you used some module like content_profile or you have altered the users table perhaps .. ?

Any ways, the $user object wont get your "integer field" directly as the user object prints all the data from the users table itself.

If your integer field is part of a node that user has created, try

global $user;
$nid = db_result(db_query( /*This is where you get the nid of the node*/ ));
$node = node_load($nid);
//then we print the field you want 
print $node->integer_field;  
// or if its not a node then simple print $variable; will do.

The above code needs to go in a block or some tpl that will print it in your header.

Please give more info if the above is not correct...

1
  • Thanks. To clarify what I'm looking for, I just need a way to print out a custom user field. I'm using Drupal 7 and I made a field connected to my users that is of type "integer".
    – oobie11
    Commented Dec 15, 2011 at 22:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.