diff --git a/lib/Drupal/profile2/Tests/ProfileCRUDTest.php b/lib/Drupal/profile2/Tests/ProfileCRUDTest.php index ab399a2..8cf3e85 100644 --- a/lib/Drupal/profile2/Tests/ProfileCRUDTest.php +++ b/lib/Drupal/profile2/Tests/ProfileCRUDTest.php @@ -74,9 +74,11 @@ class ProfileCRUDTest extends DrupalUnitTestBase { $this->assertIdentical($profile->changed, REQUEST_TIME); // List profiles for the user and verify that the new profile appears. - $list = profile2_load_by_user($this->user1); + $list = entity_load_multiple_by_properties('profile2', array( + 'uid' => $this->user1->uid, + )); $this->assertEqual($list, array( - $profile->bundle() => $profile, + $profile->id() => $profile, )); // Reload and update the profile. @@ -101,18 +103,22 @@ class ProfileCRUDTest extends DrupalUnitTestBase { $user1_profile2 = $profile; // List profiles for the user and verify that both profiles appear. - $list = profile2_load_by_user($this->user1); + $list = entity_load_multiple_by_properties('profile2', array( + 'uid' => $this->user1->uid, + )); $this->assertEqual($list, array( - $user1_profile1->bundle() => $user1_profile1, - $user1_profile2->bundle() => $user1_profile2, + $user1_profile1->id() => $user1_profile1, + $user1_profile2->id() => $user1_profile2, )); // Delete the second profile and verify that the first still exists. $user1_profile2->delete(); $this->assertFalse(entity_load('profile2', $user1_profile2->id())); - $list = profile2_load_by_user($this->user1); + $list = entity_load_multiple_by_properties('profile2', array( + 'uid' => $this->user1->uid, + )); $this->assertEqual($list, array( - $user1_profile1->bundle() => $user1_profile1, + $user1_profile1->id() => $user1_profile1, )); // Create a new second profile. @@ -134,13 +140,17 @@ class ProfileCRUDTest extends DrupalUnitTestBase { // Delete the first user and verify that all of its profiles are deleted. $this->user1->delete(); $this->assertFalse(entity_load('user', $this->user1->id())); - $list = profile2_load_by_user($this->user1); + $list = entity_load_multiple_by_properties('profile2', array( + 'uid' => $this->user1->uid, + )); $this->assertEqual($list, array()); // List profiles for the second user and verify that they still exist. - $list = profile2_load_by_user($this->user2); + $list = entity_load_multiple_by_properties('profile2', array( + 'uid' => $this->user2->uid, + )); $this->assertEqual($list, array( - $user2_profile1->bundle() => $user2_profile1, + $user2_profile1->id() => $user2_profile1, )); // @todo diff --git a/lib/Drupal/profile2/Tests/ProfileEditTest.php b/lib/Drupal/profile2/Tests/ProfileEditTest.php index e04c54c..bfeb691 100644 --- a/lib/Drupal/profile2/Tests/ProfileEditTest.php +++ b/lib/Drupal/profile2/Tests/ProfileEditTest.php @@ -75,7 +75,14 @@ class ProfileEditTest extends WebTestBase { $profile = entity_create('profile2', array('type' => 'test', 'uid' => NULL)); $profile->save(); - $profiles = profile2_load_by_user($user1); + $entities = entity_load_multiple_by_properties('profile2', array( + 'uid' => $user1->uid, + )); + $profiles = array(); + foreach ($entities as $item) { + $profiles[$item->type] = $item; + } + $this->assertEqual($profiles['test']->label(), 'label', 'Created and loaded profile 1.'); $this->assertEqual($profiles['test2']->label(), 'label2', 'Created and loaded profile 2.'); @@ -83,7 +90,14 @@ class ProfileEditTest extends WebTestBase { $this->assertEqual($loaded->pid, $profile->pid, 'Loaded profile unrelated to a user.'); $profiles['test']->delete(); - $profiles2 = profile2_load_by_user($user1); + $entities = entity_load_multiple_by_properties('profile2', array( + 'uid' => $user1->uid, + )); + $profiles2 = array(); + foreach ($entities as $item) { + $profiles2[$item->type] = $item; + } + $this->assertEqual(array_keys($profiles2), array('test2'), 'Profile successfully deleted.'); $profiles2['test2']->save(); @@ -114,6 +128,13 @@ class ProfileEditTest extends WebTestBase { $this->assertTrue((bool) $new_user->status, t('New account is active after registration.')); $profile = profile2_load_by_user($new_user, 'test'); $this->assertEqual($profile->profile_fullname[LANGUAGE_NOT_SPECIFIED][0]['value'], $edit['profile_test[profile_fullname][und][0][value]'], 'Profile created.'); + + $profiles = entity_load_multiple_by_properties('profile2', array( + 'uid' => $new_user->uid, + 'type' => 'test', + )); + $profile = reset($profiles); + $this->assertEqual($profile->profile_fullname[LANGUAGE_NOT_SPECIFIED][0]['value'], $edit['profile_main[profile_fullname][und][0][value]'], 'Profile created.'); } /** @@ -141,7 +162,12 @@ class ProfileEditTest extends WebTestBase { $edit['profile_fullname[und][0][value]'] = $this->randomName(); $this->drupalPost('user/' . $user2->uid . '/edit/test', $edit, t('Save')); $this->assertText(t('Your profile has been saved.'), 'Profile saved.'); - $profile = profile2_load_by_user($user2, 'test'); + + $profiles = entity_load_multiple_by_properties('profile2', array( + 'uid' => $user2->uid, + 'type' => 'test', + )); + $profile = reset($profiles); $this->assertEqual($profile->profile_fullname[LANGUAGE_NOT_SPECIFIED][0]['value'], $edit['profile_fullname[und][0][value]'], 'Profile edited.'); $this->drupalGet('user/' . $user2->uid); diff --git a/profile2.module b/profile2.module index 59574d1..a243121 100644 --- a/profile2.module +++ b/profile2.module @@ -150,8 +150,11 @@ function profile2_menu_load($type_id, $map, $op = '') { return FALSE; } $account = $map[1]; - $profile = profile2_load_by_user($account, $type_id); - if ($op == 'edit' && empty($profile)) { + $profiles = entity_load_multiple_by_properties('profile2', array( + 'uid' => $account->uid, + 'type' => $type_id, + )); + if ($op == 'edit' && !$profile = reset($profiles)) { $profile = entity_create('profile2', array( 'type' => $type_id, 'uid' => $account->uid, @@ -280,39 +283,6 @@ function profile2_permission() { } /** - * Fetch profiles by account. - * - * @param \Drupal\user\Plugin\Core\Entity\User $account - * The user account to load profiles for. - * @param string $type_id - * (optional) If passed, only the user account's profile of the specified type - * is loaded. If omitted, all profiles of the user account are loaded. - * - * @return array|\Drupal\profile2\Plugin\Core\Entity\Profile|false - * If a $type_id was passed, the corresponding profile or FALSE, otherwise all - * existing profiles of $account keyed by profile type ID. - */ -function profile2_load_by_user(User $account, $type_id = NULL) { - if (!isset($type_id)) { - $profiles = entity_load_multiple_by_properties('profile2', array( - 'uid' => $account->id(), - )); - $return = array(); - foreach ($profiles as $profile) { - $return[$profile->bundle()] = $profile; - } - return $return; - } - else { - $profiles = entity_load_multiple_by_properties('profile2', array( - 'uid' => $account->id(), - 'type' => $type_id, - )); - return reset($profiles) ?: FALSE; - } -} - -/** * Implements hook_user_predelete(). */ function profile2_user_predelete($account) { @@ -326,7 +296,12 @@ function profile2_user_predelete($account) { */ function profile2_user_view($account, $view_mode, $langcode) { foreach (entity_load_multiple('profile2_type') as $id => $type) { - if ($profile = profile2_load_by_user($account, $id)) { + $profiles = entity_load_multiple_by_properties('profile2', array( + 'uid' => $account->uid, + 'type' => $id, + )); + + if ($profile = reset($profiles)) { if ($profile->access('view')) { $account->content['profile_' . $id] = array( '#type' => 'user_profile_category',