0

Been trying to print the profile2 fields in the user-profile.tpl. The fields are of type: taxonomy reference field and link field.

The documentation provided in drupal doesn't work.

Workarounds provided in the profile2 issue queue here and here either prints the current users value in every profile or renders it twice.

It'll really great if someone can help me with this. Thanks for your time.

In user-profile.tpl the below renders the fields twice, once within the div and other without it from "print render($user_profile);"

    <div class="athlete-info">
    <?php $user_uid = $elements['#account']->uid;
    $profile = profile2_load_by_user($user_uid, 'athlete');

     print drupal_render(field_view_field('profile2', $profile, 'field_name', 'value'));
     print drupal_render(field_view_field('profile2', $profile, 'field_twitter','value'));
     print drupal_render(field_view_field('profile2', $profile, 'field_facebook', 'value'));
     print drupal_render(field_view_field('profile2', $profile, 'field_role', 'value'));
     ?>
     </div>

But this causes an error page on other user roles who don't have to fill up the athlete profile type. :(

EDIT: I am using the profile 2 module to create a profile called athlete with few extra fields like field_facebook,field_twitter and assign them to users of a particular role.

2
  • can you show us what you have done that doesn't work in your first link, then what you did that printed the current user's value everywhere and then what you did to cause things to render twice? if so, I am sure one or several of us can point out what is sounding like an "off by one" error, eg, something like a typo or tiny mistake, in what you are doing.
    – Jimajamma
    Commented Mar 3, 2014 at 13:11
  • Thanks for the reply... gimme a few minutes will recreate and update the question.
    – jack
    Commented Mar 3, 2014 at 13:14

2 Answers 2

1

If these fields are already a part of $user_profile and what you are wanting to do is print them in another place/div on the page, you can do something like this:

<?php
  hide($user_profile['field_name']);
  hide($user_profile['field_twitter']);
  hide($user_profile['field_facebook']);
  hide($user_profile['field_role']);
?>
<div class="profile"<?php print $attributes; ?>
  <?php print render($user_profile); ?>
</div>
<div class="athelete-info">
  <?php print render($user_profile['field_name']); ?>
  <?php print render($user_profile['field_twitter']); ?>
  <?php print render($user_profile['field_facebook']); ?>
  <?php print render($user_profile['field_role']); ?>
</div>

Eg, hide() the fields you want to print elsewhere, then render() the rest of the profile and then finally render() those specific fields where you want them.

Now, if you are getting an error because these don't exist everywhere, you can do something like this:

<?php if (isset($user_profile['field_facebook']): ?>
  <?php hide($user_profile['field_facebook']); ?>
<?php endif; ?>

and a similar wrapping around the final printing (ADDITION: this appears to be unneeded as I didn't get any errors when I did a hide($user_profile['i_got_nothing_to_hide']); and a quick look at the code for hide and render shows that they pretty smart/sneaky about being passed undefined variables or keys)

Or you could check on the user role, but if this is going to begin to put a lot of php logic into the template, you may want to investigate preprocessing() it and using a special template for the user profile of the users with this/these roles.

3
  • Hey...really glad to see your detailed explanation. I went through it thoroughly and understood the logic in rendering the core user profile fields. But still not able to hide and render the profile2 fields.
    – jack
    Commented Mar 3, 2014 at 17:47
  • 1
    You had mentioned that they show up twice, so they "have" to be in $user_profile somewhere. if you put a print array_keys($user_profile) in there somewhere that might show you what you have to work with that is just subtly different here.
    – Jimajamma
    Commented Mar 3, 2014 at 18:11
  • using devel had narrowed the field variables to hide($user_profile['profile_athlete']['view']['profile2'][2]['field_youtube']); and was able to hide it for one user. Looks the profile id changes for every user. :( will dig into it. Thanks for your guidance.
    – jack
    Commented Mar 3, 2014 at 18:24
0

With a lot of help from Jimajamma and the snippet here i was able to print the profile2 fields in the user profile template like this.

 <?php  $profiles = profile2_load_by_user($account->uid, $type_name = 'athlete' ); 

 print render($user_profile['profile_athlete']['view']['profile2'][$profiles->pid]['field_facebook']);

Thanks a lot. :)

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.