Register | Login

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

Submitted by saylinux on Thu, 05/24/2012 - 20:30

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:

http://www.konordo.com/sites/all/themes/konordo/images/pre_code_bg.gif); width: 600px; overflow-x: auto; color: rgb(61, 61, 61); text-align: left; background-position: 0% 0%; background-repeat: no-repeat repeat; ">
$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:

http://www.konordo.com/sites/all/themes/konordo/images/pre_code_bg.gif); width: 600px; overflow-x: auto; color: rgb(61, 61, 61); text-align: left; background-position: 0% 0%; background-repeat: no-repeat repeat; ">
<?php
print_r($user_profile_node, TRUE);
?>

Devel module also allows an easy way to print objects:

http://www.konordo.com/sites/all/themes/konordo/images/pre_code_bg.gif); width: 600px; overflow-x: auto; color: rgb(61, 61, 61); text-align: left; background-position: 0% 0%; background-repeat: no-repeat repeat; ">
<?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.)

Drupal theme by Kiwi Themes.