I am trying to set a user reference field on a node to have a default value of the currently logged in user. I have tried the following hook_form_alter method but I am not getting anything to populate as the default value (in fact the whole user reference field disappears) .

function modulename_form_equipment_node_form_alter(&$form, &$form_state) {
  
 $form['field_test_reference'][0]['#default_value'] = $user->uid;

}

Any suggestions on how this can be accomplished?

Comments

kidd1126’s picture

$form['#node']->field_test_reference[0]['nid'] = $nid;

may help u