0

I'm working on a Drupal 6 project and we've added some fields to a user profile via CCK. What's the best way to update this information programmatically? When the user logs in I'm gathering some data from a 3rd party API that I need to update their user with. I've got the data, but the global $user object doesn't seem to have the extra fields.

2
  • Are you using Content Profile to do this?
    – mpdonadio
    Commented Sep 28, 2011 at 16:47
  • @MPD Yes, and we've attached the content type 'profile' to the users Commented Sep 28, 2011 at 16:55

1 Answer 1

0

OK, the information is stored in the node associated with the user, and not the user itself.

You essentially need to query the database to find the node for the user, load it, edit the fields, and then save out the node.

It would roughly look like

$sql = "
  SELECT nid FROM {node}
  WHERE type = '%s' AND uid = %d AND status = %d
";

$nid = (int) db_result(db_query($sql, "profile", $user->uid, 1));

$node = node_load($nid);

$node->field_foo[0]["value"] = "foo";

node_save($node);

This is a copy/edit/paste from a project of mine, so error checking and other steps have been omitted, and the code may not run as-is.

3
  • 1
    Instead of creating the query yourself, you can also use content_profile_load() to grab the content profile node
    – Laxman13
    Commented Sep 28, 2011 at 17:23
  • Thanks. The code above is heavily edited from the real version, and I think I had a good reason to do it this way at the time, but that function really is the best solution here.
    – mpdonadio
    Commented Sep 28, 2011 at 19:06
  • For whatever reason content_profile_load() always returned false, so I just did it like above. Worked great, thanks! Commented Sep 28, 2011 at 21:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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