I have created some fields, which can be displayed in other content type by using the following code in my node.tpl.php

<?php print $node->field_birthdate['und'][0]['value'] ?>

It works perfect. But when I use the same code in use_profile.tpl.php, it does not work.

Anyone tried to print the field name in use_profile.tpl.php?

Comments

faqing’s picture

Title: Can not display field-name in user_profile.tpl.php » Can not display field-name in use_profile.tpl.php
Priority: Normal » Major
Status: Closed (fixed) » Active

In short, how can I display the field name in user_profile.tpl.php?

joachim’s picture

Priority: Major » Normal
Status: Active » Fixed

You can print $profile2->fieldname, or you can just print $fieldname.

Tip: do dsm(get_defined_vars()); in a template to see everything you have access to.

faqing’s picture

Status: Fixed » Active

print $profile2->fieldname does not work.

joachim’s picture

Status: Active » Postponed (maintainer needs more info)

It worked for me.

What is use_profile.tpl.php?

kittell’s picture

Title: Can not display field-name in use_profile.tpl.php » Can not display field-name in user_profile.tpl.php

Hi Joachim. I am also not able to print $profile2->fieldname variables in user-profile.tpl.php. (We'll just add that missing 'r' now...) For example, including echo $profile2->field_profile_name_first; gives me a PHP notice: Undefined variable: profile2 in include(). echo $field_profile_name_first; does the same: Undefined variable: field_profile_name_first in include().

Looking at $variables via echo dsm($variables); gives me only $user_profile as a relevant-seeming variable. Perhaps I missed something in dsm(get_defined_vars())?

I've printed individual profile2 fields in user-profile.tpl.php in this way:

  echo $user_profile['profile_profile']['view']['profile2'][$pid]['field_profile_name_first']['#items'][0]['safe_value'];

Notes:

  • I have one profile type, profile, hence the profile_profile
  • I get the $pid for each user page by doing a db_query() to the profiles table--I get the pid for the uid that matches the user's $elements['#account']->uid.

I look forward to finding out what terribly obvious thing I've missed here...

brightbold’s picture

Status: Postponed (maintainer needs more info) » Active

@kittel - If the issue status is set to "postponed (maintainer needs more info)" and you provide additional info, you should set the status back to active so the maintainer sees your updates! (Doing this anyway even though you're not the original poster, so that your question will hopefully get an answer.)

TimG1’s picture

Hey all,

I was wondering the same thing. I'm experiencing the what kittell is in #5. Is this the way we need to access the fields at the moment or is there a more "proper way" to do this?

  echo $user_profile['profile_profile']['view']['profile2'][$pid]['field_profile_name_first']['#items'][0]['safe_value'];

Like kittell I feel like I'm missing something obvious.

Thanks for reading,
-Tim

TimG1’s picture

Hi all,

I figured out another way of doing this while trying to print out field collections. See my post here For me the key was reading up on Drupal Renderable Arrays. Please let me know if anyone knows of a better way.

Thanks for reading,
-Tim

faqing’s picture

Status: Active » Closed (fixed)

we need to define the profile type, for exmaple main profile:

print $profile['main']->field_name['und'][0]['value'];

The following code working for me:

<?php
// load profile fields
$profile = profile2_load_by_user($elements['#account']);

// then access fields like this
print $profile['main']->field_name['und'][0]['value'];
?>

Check here:
http://drupal.org/node/1178260#comment-4590366

Status: Active » Closed (fixed)