You are here

function pcp_get_user_profile_values in Profile Complete Percent 7

Same name and namespace in other branches
  1. 5 pcp.module \pcp_get_user_profile_values()
  2. 6.2 pcp.module \pcp_get_user_profile_values()
  3. 6 pcp.module \pcp_get_user_profile_values()

Return a users profile field values that have been saved for a given user.

Parameters

int $uid: The uid of the user we are returning data for.

$entity_type: The type of entity to return fields for.

$bundle: (optional) NULL The bundle to return fields for

Return value

assoc array of all profile fields for the user.

1 call to pcp_get_user_profile_values()
pcp_get_complete_percentage_data in ./pcp.module
Get the profile complete percentage data for a given user.

File

./pcp.module, line 372
Allows users with valid permissions to tag profile fields (core fields or Profile2 fields) for a users profile to be considered complete.

Code

function pcp_get_user_profile_values($uid, $entity_type, $bundle = NULL) {
  $fields = field_info_instances($entity_type, $bundle);

  // Make sure the user info is fresh and not cached.
  $user = user_load($uid, TRUE);
  $user_fields = array();

  // Grab profile values from core profile fields.
  if ($entity_type == 'user') {
    foreach ($fields as $field) {
      $user_fields[$field['field_name']] = $user->{$field['field_name']};
    }
  }
  elseif ($entity_type == 'profile2' && ($profile = profile2_load_by_user($user, $bundle))) {
    foreach ($fields as $field_name => $field_values) {
      if (isset($profile->{$field_name}) && ($field = $profile->{$field_name})) {
        foreach ($field[LANGUAGE_NONE][0] as $value) {
          if (!is_null($value)) {
            $user_fields[$field_name] = $field[LANGUAGE_NONE][0];
            break;
          }
        }
      }
    }
  }

  // Note, entity type user will return empty fields, whereas entity type
  // profile2 will only return non-empty fields.
  return $user_fields;
}