1

Im inserting into user field reference to other users. This is my code, but it is overiding all values of field, i just want too add value and keep values added before.

<?php 
global $user;

$user->field_profile_access['und'][]['target_id'] = 34;
user_save($user)


?>

1 Answer 1

1

I think this line is wrong:

$user->field_profile_access['und'][]['target_id'] = 34;

You could try with:

$user->field_profile_access['und'][] = array('target_id' => 34);

or this:

$next = count($user->field_profile_access[LANGUAGE_NONE]);
$user->field_profile_access[LANGUAGE_NONE][$next]['target_id'] = 34;

I used LANGUAGE_NONE instead of 'und' here, but that's not the problem.

Or even better, using the Entity API. That example is for a field collection, but the idea should be useful for any entity, including nodes.

1

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.