Hi guys,

as the title indicates, I am unable to print single fields in the user-profile.tpl.php. I tried many different things, but nothing happens. The tpl has been recognized, as I am able to see custom text, etc. I can print all fields with a value in it via:

<div class="profile"<?php print $attributes; ?>>
  <?php print render($user_profile); ?>
</div>

But if I try to print a single field nothing happens. I tried:

<?php
 print check_plain($user->profile_job); 
?>
<?php
 print $profile['field_job'][$lang][0]['value'] 
?>
<?php
 print check_plain($user->field_job);
?>
<?php
 print render($content['field_job']); 
?>

And I am sure I tried a few other things. The field does have a value, an it shows up in the full print mentioned above.

What am I doing wrong here? How am I able to print single fields?

Thanks for your help in advance!

Comments

pbarnett’s picture

Try installing and enabling the devel module, then copy user-profile.tpl.php to your theme folder and edit it so that it reads

<div class="profile"<?php print $attributes; ?>>
  <?php krumo($user_profile); ?>
  <?php print render($user_profile); ?>
</div>

That will show you what the data actually looks like...

Yucom’s picture

Thanks so far Pete.

I did what you said and it appears that the field I tried to print is available. Here's what it says:

field_job (Array, 16 elements)
#theme (String, 5 characters ) field
#weight (Integer) 4
#title (String, 5 characters ) Job
#access (Boolean) TRUE
#label_display (String, 5 characters ) above
#view_mode (String, 4 characters ) full
#language (String, 3 characters ) und
#field_name (String, 11 characters ) field_job
#field_type (String, 4 characters ) text
#field_translatable (String, 1 characters ) 1
#entity_type (String, 4 characters ) user
#bundle (String, 4 characters ) user

I am not good with PHP, but as far as I can say, shouldn't the code from the first post work in any way? :|

pbarnett’s picture

I guess you'd need to do
render($user_profile['field_job']);

Yucom’s picture

Thanks for your help again Pete. Sadly it didn't work. I am not sure what's the problem, as it is described in the api: http://api.drupal.org/api/drupal/modules--user--user-profile.tpl.php/7/s...

We today tried this:

<?php
$test = $user->field_job;  if(!empty($test)) echo "Yes!" else echo "No"
?>

And it said no. But the field isn't empty, as it is printed when I print them all. I really don't get it. :|

Here's a screen of what Krumo says: http://www.sternengarde.de/images/array.jpg

Yucom’s picture

Okay,

I solved it, and it was a hard lesson to learn! It took me about 8 hours to find a solution. Many thanks to Pete for his participation.

This is how you have to print single fields in your profile:

<?php if (isset($user_profile[field_NAME])): ?> // Check if value in field
<?php print render($user_profile[field_NAME])?> // print field if value
<?php endif; ?>

Thanks again and good night (it's 11 o'clock pm here),

Yucom

faqing’s picture

Hi Yucom,

It does not work in D7. Also, use-profile.tpl.php should be use_profile.tpl.php, right?
Can you help me this:
http://drupal.org/node/1108926

Thanks

Yucom’s picture

It's not use_profile.tpl.php, but user-profile.tpl.php.

Also I have to correct myself a little bit. This version is more correct:

<?php if (isset($user_profile['field_NAME'])): ?> // Check if value in field
<?php print render($user_profile['field_NAME'])?> // print field if value
<?php endif; ?>

And yes, it's working. I am using it right now. :)

faqing’s picture

The aboce codes does not work for me. Also, user-profile.tpl.php does not work for profile 2.

astanley86’s picture

This is the code I got to work for me: <?php print render($user_profile['field_user_lastname']['#items']['0']['value']);?>