You are here

function profile2_load_by_user in Profile 2 7

Same name and namespace in other branches
  1. 7.2 profile2.module \profile2_load_by_user()

Fetch profiles by account.

Parameters

$account: The user account to load profiles for, or its uid.

$type_name: To load a single profile, pass the type name of the profile to load.

Return value

Either a single profile or an array of profiles keyed by profile type.

See also

profile2_load_multiple()

profile2_profile2_delete()

Profile::save()

10 calls to profile2_load_by_user()
Profile2CRUDTestCase::testCRUD in ./profile2.test
Tests CRUD for a profile related to a user and one unrelated to a user.
profile2_by_uid_load in ./profile2.module
Menu load callback.
profile2_category_access in ./profile2.module
Menu item access callback - check if a user has access to a profile category.
profile2_form_user_profile_form_alter in ./profile2.module
Implements hook_form_FORM_ID_alter() for the user edit form.
profile2_from_user_context in plugins/relationships/profile2.inc
Return a new context based on an existing context.

... See full list

2 string references to 'profile2_load_by_user'
profile2_profile2_delete in ./profile2.module
Implements hook_profile2_delete().
Profile::save in ./profile2.module
Permanently saves the entity.

File

./profile2.module, line 268
Support for configurable user profiles.

Code

function profile2_load_by_user($account, $type_name = NULL) {

  // Use a separate query to determine all profile ids per user and cache them.
  // That way we can look up profiles by id and benefit from the static cache
  // of the entity loader.
  $cache =& drupal_static(__FUNCTION__, array());
  $uid = is_object($account) ? $account->uid : $account;
  if (!isset($cache[$uid])) {
    $cache[$uid] = db_select('profile', 'p')
      ->fields('p', array(
      'type',
      'pid',
    ))
      ->condition('uid', $uid)
      ->execute()
      ->fetchAllKeyed();
  }
  if (isset($type_name)) {
    return isset($cache[$uid][$type_name]) ? profile2_load($cache[$uid][$type_name]) : FALSE;
  }

  // Return an array containing profiles keyed by profile type.
  return $cache[$uid] ? array_combine(array_keys($cache[$uid]), profile2_load_multiple($cache[$uid])) : $cache[$uid];
}