3

I am trying to programmatically change a custom field in a user object and I'm having very little success. I want to just use a function to set the field for the object, like field_get_items, except a setter version.

It seems like I could do:

$account = user_load($GLOABALS['user']->uid);

$edit = array(
    'field_custom' => array(
        'und' => array(
            0 => array(
                'value' => $lead_id
            )
        )
    )
);

user_save($account, $edit)

But there must be a better way to do this.

Any help would be appreciated. Thanks!

9

Use the Entity module wrapper.

The entity API provides some wrapper classes you may use to easily deal with entities and to leverage the entity property information modules provided. With the help of the wrappers you can access the property information, loop over known properties, or just get/set the described data values, etc.

Install the Entity module and do something like:

$user = user_load($GLOBALS['user']->uid);
$obj = entity_metadata_wrapper('user', $user);
$obj->field_custom = $lead_id;
$obj->save();

$GLOBALS is http://php.net/manual/en/reserved.variables.globals.php

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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