1

So I know that the $user_profile is basically an array. And when I use the DSM command with devel I saw all that info in the array, but how on earth do I begin extracting and displaying data from that?

I'm still not too keen on php, trying to learn as I go and on the side. I understand the concepts of an array and how to get top level stuff. But for those items embedded in other arrays embedded in yet more arrays, I'm not sure how to get to those and display them.

For a simple instance I just want to take the username of the profile currently being views and display it on a custom user-profile.tpl page. And I see it with devel, but I don't know how to get it.

1

In drupal 7 on the user-profile.tpl.php to which you refer the whole user object is loaded so you can output the different pieces you see in devel by doing this (a modification of the default user-profile.tpl.php you've copied to your theme):

Default:

<div class="profile"<?php print $attributes; ?>>
  <?php print render($user_profile); ?>
</div>

Modified:

<div class="profile"<?php print $attributes; ?>>
  <?php print render($user->name); ?>
  <?php print render($user->signature); ?>
  <?php print render($user->timezone); ?>
  //and so on...
</div>

On Drupal 6 the only difference is

<?php print $user->name; ?> 
//dropping the render wrapper (which is good practice in D7 but is not used in 6)

As the questioner mentioned in the comments the above will display the currently logged in user's info. To get the info of the user's page you are visiting (ex. YOUR_BASE_URL/user/2) you can do this (this will also work with aliased user pages):

<?php 
   /*call this ($user_page_object) whatever you want but it is ill advised to
     call it $user as that will conflict with the $user object for the currently 
     logged in user*/
   $user_page_object = user_load(arg(1)); 
?>
<div class="profile"<?php print $attributes; ?>>
   <?php print render($user_page_object->name); ?>
   <?php print render($user_page_object->signature); ?>
   <?php print render($user_page_object->timezone); ?>
   //and so on...
</div>

And to access additional fields you may have added you can drill into the $user_profile array which contains those fields. (Which would've been added here admin/config/people/accounts/fields).

I recommend using the devel module to kpr or dpm (I prefer kpr) to look into those two vars, ie:

<?php 
kpr($user_page_object); 
kpr($user_profile);
?>
  • Okay now this would work however it shows my user name on everyones profile, lol – Jordan Aug 13 '12 at 3:43
  • What are you using to create these users? Because the above shows the actually user name that created the user. Are you on D6 or D7? – nedwardss Aug 13 '12 at 14:57
  • I'm in d7. And I now see that this method shows the user that is logged in's info. Good for my module I'm developing, but not for general user-profile theming. I need help drilling down in the user_profile array to get the info out that I need and then printing that on the page. – Jordan Aug 14 '12 at 16:33
  • I updated my answer to address the comment above. Hope it helps. – nedwardss Aug 14 '12 at 19:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.