diff --git a/lib/Drupal/profile2/Profile.php b/lib/Drupal/profile2/Profile.php index 18129e4..1755081 100644 --- a/lib/Drupal/profile2/Profile.php +++ b/lib/Drupal/profile2/Profile.php @@ -146,11 +146,29 @@ class Profile extends Entity { public function buildContent($view_mode = 'full', $langcode = NULL) { $content = array(); + // Assume newly create objects are still empty. if (!empty($this->is_new)) { $content['empty']['#markup'] = '' . t('There is no profile data yet.') . ''; } - return entity_get_controller($this->entityType)->buildContent($this, $view_mode, $langcode, $content); + + if (!isset($langcode)) { + $langcode = language(LANGUAGE_TYPE_CONTENT)->langcode; + } + + // Allow modules to change the view mode. + $context = array('langcode' => $langcode); + drupal_alter('entity_view_mode', $view_mode, $this, $context); + + // Build fields content. + // In case of a multiple view, node_view_multiple() already ran the + // 'prepare_view' step. An internal flag prevents the operation from running + // twice. + field_attach_prepare_view('profile2', array($this->id() => $this), $view_mode, $langcode); + entity_prepare_view('profile2', array($this->id() => $this), $langcode); + $content += field_attach_view('profile2', $this, $view_mode, $langcode); + + return $content; } public function save() { diff --git a/lib/Drupal/profile2/Tests/Profile2CRUDTestCase.php b/lib/Drupal/profile2/Tests/Profile2CRUDTestCase.php index a3299c8..78a1790 100644 --- a/lib/Drupal/profile2/Tests/Profile2CRUDTestCase.php +++ b/lib/Drupal/profile2/Tests/Profile2CRUDTestCase.php @@ -3,13 +3,15 @@ namespace Drupal\profile2\Tests; use Drupal\simpletest\WebTestBase; +use Drupal\profile2\Profile; +use Drupal\profile2\ProfileType; /** * Test basic CRUD functionality. */ class Profile2CRUDTestCase extends WebTestBase { - public static $modules = array('profile2', 'locale'); + public static $modules = array('profile2', 'config'); public static function getInfo() { return array( @@ -23,16 +25,16 @@ class Profile2CRUDTestCase extends WebTestBase { parent::setUp(); profile2_type_save(new ProfileType(array( - 'type' => 'test', + 'id' => 'test', 'label' => 'label', 'weight' => 0 - ))); + ), 'profile2_type')); profile2_type_save(new ProfileType(array( - 'type' => 'test2', + 'id' => 'test2', 'label' => 'label2', 'weight' => 2 - ))); - profile2_load_multiple(FALSE, array(), TRUE); + ), 'profile2_type')); + profile2_load_multiple(array(), TRUE); // Add a field to main type, which is created during module installation. $field = array( @@ -87,8 +89,9 @@ class Profile2CRUDTestCase extends WebTestBase { // Delete a profile type. profile2_type_load('test')->delete(); + // Try deleting multiple profiles by deleting all existing profiles. - $pids = array_keys(profile2_load_multiple(FALSE)); + $pids = array_keys(profile2_load_multiple()); profile2_delete_multiple($pids); } @@ -97,17 +100,16 @@ class Profile2CRUDTestCase extends WebTestBase { */ function testRegistrationIntegration() { // Allow registration by site visitors without administrator approval. - variable_set('user_register', 1); + config('user.settings')->set('register', USER_REGISTER_VISITORS)->save(); $edit = array(); $edit['name'] = $name = $this->randomName(); $edit['mail'] = $mail = $edit['name'] . '@example.com'; $edit['profile_main[profile_fullname][und][0][value]'] = $this->randomName(); $this->drupalPost('user/register', $edit, t('Create new account')); $this->assertText(t('A welcome message with further instructions has been sent to your e-mail address.'), t('User registered successfully.')); - $return = user_load_multiple(array(), array('name' => $name, 'mail' => $mail)); - $new_user = reset($return); - $this->assertTrue($new_user->status, t('New account is active after registration.')); - $this->assertEqual(profile2_load_by_user($new_user, 'main')->profile_fullname[LANGUAGE_NONE][0]['value'], $edit['profile_main[profile_fullname][und][0][value]'], 'Profile created.'); + $new_user = user_load_by_name($name); + $this->assertTrue((bool) $new_user->status, t('New account is active after registration.')); + $this->assertEqual(profile2_load_by_user($new_user, 'main')->profile_fullname[LANGUAGE_NOT_SPECIFIED][0]['value'], $edit['profile_main[profile_fullname][und][0][value]'], 'Profile created.'); } /** @@ -117,6 +119,7 @@ class Profile2CRUDTestCase extends WebTestBase { user_role_revoke_permissions(DRUPAL_AUTHENTICATED_RID, array('edit own main profile', 'view own main profile')); $user1 = $this->drupalCreateUser(); $this->drupalLogin($user1); + // Make sure access is denied to the profile. $this->drupalGet('user/' . $user1->uid . '/edit/main'); $this->assertText(t('Access denied'), 'Access has been denied.'); @@ -134,7 +137,7 @@ class Profile2CRUDTestCase extends WebTestBase { $edit['profile_main[profile_fullname][und][0][value]'] = $this->randomName(); $this->drupalPost('user/' . $user2->uid . '/edit/main', $edit, t('Save')); $this->assertText(t('The changes have been saved.'), 'Profile saved.'); - $this->assertEqual(profile2_load_by_user($user2, 'main')->profile_fullname[LANGUAGE_NONE][0]['value'], $edit['profile_main[profile_fullname][und][0][value]'], 'Profile edited.'); + $this->assertEqual(profile2_load_by_user($user2, 'main')->profile_fullname[LANGUAGE_NOT_SPECIFIED][0]['value'], $edit['profile_main[profile_fullname][und][0][value]'], 'Profile edited.'); $this->drupalGet('user/' . $user2->uid); $this->assertText(check_plain($edit['profile_main[profile_fullname][und][0][value]']), 'Profile displayed.'); diff --git a/profile2.module b/profile2.module index 66b5c7f..f37bcb0 100644 --- a/profile2.module +++ b/profile2.module @@ -404,7 +404,7 @@ function profile2_type_delete(ProfileType $type) { */ function profile2_profile2_type_delete(ProfileType $type) { // Delete all profiles of this type. - $pids = array_keys(profile2_load_multiple_by_properties(array('type' => $type->type))); + $pids = array_keys(profile2_load_multiple_by_properties(array('type' => $type->id()))); if ($pids) { profile2_delete_multiple($pids); } @@ -422,7 +422,7 @@ function profile2_user_view($account, $view_mode, $langcode) { '#title' => $profile->label, '#prefix' => '', ); - $account->content['profile_' . $type]['view'] = $profile->view('account'); + $account->content['profile_' . $type]['view'] = $profile->buildContent('account'); } } } @@ -511,7 +511,7 @@ function profile2_attach_form(&$form, &$form_state) { } } $form['#validate'][] = 'profile2_form_validate_handler'; - $form['#submit'][] = 'profile2_form_submit_handler'; + $form['actions']['submit']['#submit'][] = 'profile2_form_submit_handler'; } /**