Drupal - Content Profile Node from UID

in

Q: How do I load a user's Content Profile node in PHP given their UID or user object?
A: Use the function content_profile_load() to load the associated profile node.

The Content Profile module is very handy, it allows you to create user profiles using the CCK and all of the nice field types and display options that comes with it - often it is a much better option than the default drupal profile module, using it you should be able to craft a much more sophisticated user profile & profile page that probably fits in better with your target web design.

But given a user, or user id (uid) how is it possible to programmatically load the user's associated profile node, there is no mention of it in the user object?

Luckly the module provides a function to do this for you:

content_profile_load($type, $uid, $lang = '')

(details here).

the $type argument is the content type name used to store the profile data, by default this is just 'profile'. The second argument takes the target user id. So it could be called like this:

  $profile_node = content_profile_load('profile', $user->uid);

or if you already have the UID:

  $profile_node = content_profile_load('profile', $uid);

Aftr that $profile_node is just a regular node and its fields can be queried in the usual way. A handy way see what its fields are is to install the devel module and pop dvm($profile_node); somwehere in your code, this should print the variable's structure to the screen.

I had to search for how to do this for a while, so I hope this post helps somebody in a similar situation!

Stranger
Stranger's picture
It helps a lot!

Thanks for making this post. Rated 5 Stars.. Keep it up!

web design singapore
http://www.webcada.com
web design singapore

Pingback

[...] http://www.pridedesign.ie/content/drupal-content-profile-node-uid digg_url = 'http://www.pridedesign.ie/content/drupal-user-content-profile'; digg_title = 'Drupal - User from Content Profile'; digg_bodytext = "Q: How do I get the user id that is associated with a Content Profile node, it doesn\'t seem to have any obvious referenced user field?\r\n\r\nA: The user that a Content Profile node refers to is given by the node\'s author field! It"; digg_skin = 'standard'; digg_bgcolor = '#000000'; [...]

Stranger
Stranger's picture
Thanks for the tip. I was

Thanks for the tip. I was struggling with this problem for a few days.

Tudor
Web Content Management System

Stranger
Stranger's picture
Interesting

Do I have to load some module before using this function? I've tried using it in template.php with no luck. Any ideas anyone? Thanks.

ALTASO - Alberta Software & Drupal Development

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can use BBCode tags in the text.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Copy the characters (respecting upper/lower case) from the image.

Pride on FacebookPride on TwitterPride on TwitterPride on Twitter

Join Our Newsletter

* indicates required

Irish Internet Association

finalist.png
phone-number.png