According to the user-profile.tpl.php documentation you can use $profile to show profile categories and their items. http://api.drupal.org/api/file/modules/user/user-profile.tpl.php/6. For example, if a site is configured with a category of "contact" with fields for of addresses, phone numbers and other related info, then doing a straight print of $profile['contact'] will output everything in the category.

I've been trying to figure out how I can just show a single field from a particular category. Say I have a category 'Basic Information' and it has a field called 'Display Name'. How can I just show the field 'Display Name' without spitting out the rest of the fields in that category?

I appreciate any input to help me convert my custom user profiles to drupal 6.

Comments

dvessel’s picture

The docs in that template file points to this link:

http://api.drupal.org/api/file/modules/user/user-profile-category.tpl.php/6

That's where each category is assembled. To target a specific category, I would add a template suggestion for it. None are provided for the category template in core but it should be easy to add.

http://drupal.org/node/223440

joon park

mariusooms’s picture

I understand that in both user-profile-category.tpl.php and user-profile-item.tpl.php the categories and fields are assembled. Then they are processed through the user-profile.tpl.php. But I don't see how you can gain control over outputting individual fields to the user profile like we could in Drupal 5.

Before I could print check_plain($user->profile_display_name) using these snippets:
http://drupal.org/node/35728
http://drupal.org/node/35730

I'm not getting how I can output these same values with the Drupal 6 user-profile.tpl.php.

I thought it would be something like:

print check_plain($profile['Basic Information']['Display Name'])

Where 'Basic Information' is the category and 'Display Name' is a field. However this is a no show.

I also have other fields in this category I don't need to show, therefore I need individual control on displaying fields. An example how to code it would be great, I am not an expert coder and fairly new at Drupal. Thanks again.

dvessel’s picture

Are you using devel.module? It can help here by showing you the variables.

module page: http://drupal.org/project/devel
screen cast: http://drupal.org/node/209561

The docs inside the category template points to its preprocessor.

http://api.drupal.org/api/function/template_preprocess_user_profile_cate...

See how $variables['profile_items'] = $variables['element']['#children']; is set? The #children is the whole category rendered out. You have to back out and dig into $variables['element'] and selectively print on the elements. I'll leave that up to you. It's not hard to do.

But here is how you would selectively work with templates:

function phptemplate_preprocess_user_profile_category(&$vars) {
  // check title.
  if (isset($vars['element']['#title'])) {
    // Clean title then add as a template suggestion.
    // For example, if the title is "History" then a suggestion
    // of "user-profile-category-history.tpl.php" will be made.
    $string_clean = preg_replace('![^abcdefghijklmnopqrstuvwxyz0-9-_]+!s', '-', drupal_strtolower($vars['element']['#title']));
    $vars['template_files'][] = 'user-profile-category-'. $string_clean;
  }
}

Read over the 6 theming docs, and figure it out the rest. http://drupal.org/node/171179

joon park

mariusooms’s picture

First off thanks for trying to help Joon, but I really feel I'm going from something really simple to really complicated. I realize most of this stuff is easy as pie to the seasoned drupal themer, but my head starts to hurt trying to make sense of these functions. I'm still very much lost and almost feel if I'm not communicating clearly what I'm trying to do.

In my sub-theme folder I created a user-profile.tpl.php to take control of the user profiles page. I created some custom fields under a new category. I don't understand why it has become so complicated to output a single field unto the profile page in Drupal 6, when it was so easy in Drupal 5. The theming described in the nodes above is way beyond what I can do at the moment. However I was able to grasp the previous implementation of a custom user profile.

This example is working in Drupal 6 in user-profile.tpl.php:

print check_plain($user->name)

How come I can't use the following as well in Drupal 6 (this worked in Drupal 5):

print check_plain($user->profile_display_name)

I know it is hard for us beginners to grasp Drupal, I appreciate you being patient with me.

dvessel’s picture

mariusooms, have you used the devel module? A few things have changed and yes, it's not as easy to get to the field from user-profile.tpl.php. I alluded to doing it from user-profile-category.tpl.php.

1.) Copy the snippet of code I pasted above into your theme.

2.) Copy the template file from "modules/user/user-profile-category.tpl.php" into your theme.

3.) Make a duplicate of the same file and name it based on the category your trying to target. Example: "user-profile-category-my-category.tpl.php". The is the file you will be working with but don't remove the "user-profile-category.tpl.php" template. Both are needed.

clear the registry!

4.) Use devel themer module to peek into the available variables for the file. Visit the page and enable it, click around the page till you find your specific category template. Your field should be listed in there somewhere. The variables will also be listed. Start poking and look for it.

Once you find the exact field, it may be a simple as this from the template:

print $element['profile_display_name']['#value'];

Why is it more difficult? I think it's just different. With the devel module, it should become clear.

Yeah, I think this could be improved. But the change is partly due to how the data is handled. Drupal is trying to move towards a consistent data structure for the output. It now works with drupal_render. It won't make much difference to most right now but in the future, everything should be handled the same way. This makes it consistent and learning it once will mean knowing how to apply it elsewhere. sorry for not making much sense. Just making this note for anyone who knows more about the internals. The instructions above should be enough to get you going. –I hope.

joon park

mariusooms’s picture

Thanks Joon...when walking through these motions it's starting to make sense slowly. I worked it out and am very happy to say I can keep developing for our future release on Drupal 6. I appreciate you taking me through these steps, problem solved!

Side note: With devel enabled, I get a " The requested page could not be found" error when I click on the 'edit' tab for the user profile. When I disable the devel module it works fine again.

Wolfflow’s picture

Hi dvessel,

appreciate very much your help. just as confirmation:

This function goes to template.php for my new theme? :

<?php
function phptemplate_preprocess_user_profile_category(&$vars) {
  // check title.
  if (isset($vars['element']['#title'])) {
    // Clean title then add as a template suggestion.
    // For example, if the title is "History" then a suggestion
    // of "user-profile-category-history.tpl.php" will be made.
    $string_clean = preg_replace('![^abcdefghijklmnopqrstuvwxyz0-9-_]+!s', '-', drupal_strtolower($vars['element']['#title']));
    $vars['template_files'][] = 'user-profile-category-'. $string_clean;
  }
}
?>

Note: Developing custom User-profile template.

;-)

There is nothing better then Drupal. Please contribute
Alek Theme Developing
Drupal Themes Developing

Contact me for drupal projects in English, German, Italian, Drupal Hosting Support.

tedece’s picture

Yes is this !
At the end of the template.php file

I have delete the <?php and ?> in my case

dominich’s picture

I'm tearing my hair out a bit here too (if I had any, obv)... I'm trying to pull a custom profile field from the user profile (for example 'profile_personal_firstname' and drop it into a page or a block (NOT the user profile page).

Simplest example would be to have a block in the left hand column saying 'Hello Dominic', pulling the 'dominic' from the custom profile field.

Surely I'm missing something really obvious here - tried including global $user and $ profile fields, pplus all of the info above.

Any help truly appreciated - apologies if this is an obvious problem.

Dominic.

mariusooms’s picture

If it's in a block, you've got to consider on what type of node it is displayed. Either 'user', 'node', 'blog' etc. Also you have to consider who's profile field you'd like to show, either the logged in user or the current user.

This snippet below will show your 'profile_personal_firstname' as the logged in user.

<?php
    global $user;
    print $user->profile_personal_firstname;
?>

It is a little more involved if you'd like to show the name of the current user, which involves loading the user variables.

Hope this helps.

Marius

dominich’s picture

that's what I figured, but for some reason it's not working in my Drupal 6 install.

If I add a page, set the input format to PHP code and drop that code in it displays nothing. Should that definitely work on Drupal 6? It's bizarre but nothing's showing up.

Will write a custom database lookup to do it, but surely there should be no need...

dominich’s picture

<?php
global $user;
profile_load_profile($user);
echo $user->profile_personal_firstname;
?>

phew!

tedece’s picture

Thanks for this explanation !
i have pass much time on this but now it's works fine !

tedece’s picture

If i don't want to display the empty field ?
it was like this for Drupal 5

<?php if($user->profile_postcode) { ?>
<div class="fields">Postcode: <?php print check_plain($user->profile_postcode) ?></div>
<?php }?>

What i have to do for Drupal ?
I have this ofr the moment...
I understand that i have to put a if before..

  <?php
print $element['profile_weburl']['#value'];
?>
tedece’s picture

Ok i have understand...
You have to write this, it's really easy indeed

<?php if($element['profile_pays']['#value']) { ?>
<p>
<?php
print $element['profile_pays']['#value'];
?>
</p>
<?php }?>
lelizondo’s picture

subscribing

Luis

JoshG-1’s picture

In Drupal 6.x, if you want to actually manipulate variables yourself in the aforementioned file:

print $account->key

where 'key' is one of

  • name
  • pass
  • mail
  • mode
  • sort
  • threshold
  • theme
  • signature
  • created
  • access
  • login
  • status
  • timezone
  • language
  • picture
  • init
  • data
  • timezone_name
  • form_build_id
  • picture_delete
  • picture_upload
  • roles
  • userreference
  • content

To see everything available, add the following function to template.php and open a user page:

function phptemplate_preprocess_user_profile(&$variables) {
   print "<pre>";
   print_r(element_children($variables['account']));
   print "</pre>";
}
Michsk’s picture

Thanks josh thats a great reply!