Last updated November 13, 2009. Created on November 13, 2009.
Edited by Tim_O. Log in to edit this page.

Here's a short description on how to modify any profile-fields that you have created with the profile-module included in the Drupal core (at ?q=admin/user/profile). You need to know the name of the profile-field (starting with "profile_") and the category of that field (a name that you must provide when creating it). I will assume profile_test as the name and "test-category" as the category in the following, you will need to adjust these to modify fields with an other name/category.

Now, create a rule, in my case I used the trigger "user account has been created" to create a triggered rule. Then add an action and choose "PHP/Execute Custom PHP code". Now first have a look at the section "PHP-Evaluation" and find what variables of type user you have here. You will probably always $user as the acting user but may have something like $account as for my just created account (remember the trigger I used). You will need to use the right variable for your purpose here.

Now provide the following as PHP code:

$edit = array( 'profile_test' => 1 );
profile_save_profile($edit, $account, "test-category");

Remember to set $account, profile_test, and test-category according to your needs. Voila, each time the trigger gets executed, your profile-field will get set. You can of course use a variable instead of the static 1 above.

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

OliverColeman’s picture

I'm pretty sure the above will not work for profile categories containing multiple fields. Any fields in the specified category not specified in the $edit array above will end up being cleared (one of the great benefits of the Profile module...).

I use my own functions to get and set profile field values:

/*
* Get the profile field id for the given field name.
*/
function co_get_profile_field_fid($name) {
  return db_result(db_query("SELECT fid FROM {profile_fields} WHERE name='%s'", $name));
}

/*
* Get the profile field value for the given user and field. $field can be a field id or field name.
*/
function co_get_profile_field_value($uid, $field) {
  if (!is_numeric($field)) {
    $field = co_get_profile_field_fid($field);
  }
  return db_result(db_query("SELECT value FROM {profile_values} WHERE fid=%d AND uid=%d", $field, $uid));
}

/*
* Set the profile field value for the given user and field. $field can be a field id or field name.
*/
function co_set_profile_field_value($uid, $field, $value) {
  if (!is_numeric($field))
    $field = co_get_profile_field_fid($field);
  db_query("DELETE FROM {profile_values} WHERE fid=%d AND uid=%d", $field, $uid);
  db_query("INSERT INTO {profile_values} (fid, uid, value) VALUES (%d, %d, '%s')", $field, $uid, $value);
}

joeybaker’s picture

This works very well. It's unfortunate that it has to query the database twice to accomplish the latter two functions, but … meh. Thank you!