0

I am tring to load a full profile2.

I a trying the following code

<?php 
$uid = user_load($node->uid);
$profile2_p = profile2_load_by_user($uid, 'profile2_profile');
print render($profile2_p);
?>

I get an error saying object can't be used as array.

If I print_r $profile2_p then I get an array relating to the correct profile2 entity. How can I render this entity?

Thanks

1 Answer 1

2

Looks like you can get a renderable array using profile2_view():

/**
 * View a profile.
 *
 * @see Profile::view()
 */
function profile2_view($profile, $view_mode = 'full', $langcode = NULL, $page = NULL) {
  return $profile->view($view_mode, $langcode, $page);
}

https://git.drupalcode.org/project/profile2/-/blob/7.x-1.x/profile2.module#L1084

See also that modules hook_user_view() which does the same:

https://git.drupalcode.org/project/profile2/-/blob/7.x-1.x/profile2.module#L399

/**
 * Implements hook_user_view().
 */
function profile2_user_view($account, $view_mode, $langcode) {
  foreach (profile2_get_types() as $type => $profile_type) {
    if ($profile_type->userView && $profile = profile2_load_by_user($account, $type)) {
      if (profile2_access('view', $profile)) {
        $account->content['profile_' . $type] = array(
          '#type' => 'user_profile_category',
          '#title' => $profile_type->getTranslation('label'),
          '#prefix' => '<a id="profile-' . $profile->type . '"></a>',
        );
        $account->content['profile_' . $type]['view'] = $profile->view($view_mode);
      }
    }
  }
}

With a single entity in your code, you can:

render($profile2_p->view($some_view_mode));

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.