Printing or Rendering a Node Field or Profile2 Field in Drupal 7
Mike Milano — October 20, 2011 - 1:17pm
When overriding a node template or user profile, you should not be drilling down the field array and printing the output unless you have a very good reason to do so. i.e. (do not do this) print $node->field_zipcode['und']['0']['value'];
The Drupal 7 field api offers us the field_view_field() function to get a renderable array of the output for a field entity.
Using this method also give you access to whatever settings the field has setup in the "Manage Display" tab of the entity.
This is an example of how to print a node field named "field_zipcode".
<?php print drupal_render(field_view_field('node', $node, 'field_zipcode')); ?>
In that example, 'node' is the entity type. field_view_field() works with other entity types as well, such as profile2.
Here is an example of how to print a profile2 field named 'field_zipcode' that exists in the 'main' profile2 type.
<?php $profile = profile2_load_by_user($account); print drupal_render(field_view_field('profile2', $profile['main'], 'field_zipcode')); ?>

ha - mvc and stuff like this
Melvin (not verified) — February 14, 2012 - 4:59pmha - mvc and stuff like this in modern frameworks - and drupal need instructions like this - the worst stuff i've seen for a long time ...
Hi Mike, Thanks for the post.
Lewis (not verified) — January 21, 2012 - 1:00pmHi Mike,
Thanks for the post. I just recently migrated to Drupal 7 and am still new to Profile2 module. I would like to print fields specific to Profile2 by overriding user-profile.tpl.php
I placed the code:
Where "main" is my profile type name and "field_profile_first" is a text field for someone's first name. I keep getting an error that the variable "account" is undefined. Is there something I may be missing to print Profile2 fields in user-profile.tpl.php? Thanks!
good question, if you don't
Mike Milano — January 30, 2012 - 7:29pmgood question, if you don't already have an account variable, you can create one with a user id with user_load().
Hi Chris, Could it be you
Mike Milano — December 14, 2011 - 4:52amHi Chris,
Could it be you were just missing the drupal_render() ?
I added a text field to users using the default D7 method. I tested this below successfully.
Note that the first argument for field_view_field() is the entity type. If ever in doubt, you can open up the database and look at the table. (field_data_field_user_phone) Each record has an entity type, and in this case, it was "user".
If that doesn't work for you then make sure $user has a ->uid in it. If it still doesn't work, test it with another field, that is a text field, just for a sanity check.
So what if you are NOT using
chris (not verified) — December 11, 2011 - 2:16pmSo what if you are NOT using Profile2, but only D7 CORE's default fieldable user entity? Sure, I can access data using:
but when I try to use field_view_field() I run into problems. The following don't work:
as well as:
What is the correct way to access this user data in D7?
Post new comment