Drupal - Load user's Content Profile, given the UID

User login

There are so many reasons to use Content Profile instead of the user's core profile. For once, profiles are created as separate nodes and therefore the full flexibility of CCK and VIews come in the game. This way it is possible to build some really sophisticated profiles, which is usually what is needed for any website.

I had to look around for an easy solution on how to load the user's content profile programmatically, so the following might come in helpful.

It is a good thing that the CT module includes a function for this specific reason: content_profile_load($type, $uid, $lang = '')

The $type argument is the name chosen for the content type that holds the profiles. (Default is 'profile'). The second argument is the user id. If there are more than one profiles available, the function can be called more than once, of course, while changing the $type argument.

And here is an example on how to use it:


<?php $user_profile_node = content_profile_load('profile', $user->uid); ?>

At this point, the variable $user_profile_node is a node object, that holds the user's content profile. In order to see the contents of this, you could easily print the object, like this:

<?php
print_r
($user_profile_node, TRUE);
?>

Devel module also allows an easy way to print objects:

<?php dvm($user_profile_node); ?>

And that's it! We have successfully loaded the user's content profile in a variable.
We can now use all the available fields, and the nid is also available under $user_profile_node->nid (in case we need to use it as e.g. an argument.)

Tags: 
Drupal 6, PHP / MySQL