I'm creating some fields (term reference, image) on main profile type(profile2).
So, in the user-profile.tpl.php template i'm using this code for print some fields in profile page:
$profile = profile2_load_by_user($elements['#account']);
print render(field_view_field('profile2', $profile['profile'], 'field_country', 'value'));
print render(field_view_field('profile2', $profile['profile'], 'field_gender', 'value'));
$profile = profile2_load_by_user($elements['#account']);
print render(field_view_field('profile2', $profile['profile'], 'field_front_image', 'value'));
The profile type is "profile", i change its name from "main" to "profile". The fields to print are:
field_country (with "Countries" module).
field_gender (term reference, with taxonomy term).
field_front_image (image)
But, the code work it only if the field is defined. So, if the field is not selected for user or it's empty, the following error is displayed in the profile page:
Notice: Undefined index: profile in include() (line 55 of .../public_html/sites/all/themes/***/user-profile.tpl.php).
EntityMalformedException: Missing bundle property on entity of type profile2. en entity_extract_ids() (line 7562 of .../public_html/includes/common.inc).
Is there any solution?
Comments
Comment #1
firfin commentedJust check if the entity is empty before you use it? See #1328534: EntityMalformedException
You can perform a similar check for specific fields before trying to use them.
Marked this a duplicate.
Comment #2
prezaeis commentedHia, Im trying to achieve the same thing, i took a look at ur link u provided but couldnt figure out how to implement it in to my code, do u mind helping me? Im trying to print fields from the users profile2 profile on to the user-profile.tpl.php page, im using the code below but i only want it executed if its not empty. Any ideas?
Thank u in advance