2

I am trying to access my profile2 fields in template.php. I get profiledata, but I also get an error message:

EntityMalformedException: Missing type profile2. in entity_extract_ids() (row 7663 of /includes/common.inc).

Should not the entity type in field_get_items() be 'profile2', or is it another value?

$profiledata = profile2_load_by_user($variables['comment']->uid);
$lastname = field_get_items('profile2', $profiledata, 'field_lastname');

2 Answers 2

4

profile2_load_by_user() takes a user account (not just a uid) as its first argument, so you'll need to use:

$account = user_load($variables['comment']->uid);
$profiledata = profile2_load_by_user($account);
$lastname = field_get_items('profile2',$profiledata,'field_lastname');
5

As @Clive mentioned profile2_load_by_user() takes user account object not uid, but you also need type name as second argument to retrieve field_lastname using field_get_items function.

I recommend you to use this function profile2_by_uid_load instead of profile2_load_by_user

So your code should look like the following:

$profiledata = profile2_by_uid_load($variables['comment']->uid, 'profile_type_name');

$lastname = '';

if($profiledata) {

  $lastname  = field_get_items('profile2', $profiledata, 'field_lastname');
}
1
  • The returned value is an array. So you should add "$lastname = $lastname[0]['safe_value'];" before the last line.
    – Druvision
    Commented Oct 13, 2013 at 13:09

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.