0

I am trying to set the value for an existing Node Reference drop-down field in a content type via hook_form_alter as part of an AJAX call. The form needs to set values based on a selection, which is does correctly for dozens of fields, but I can't get or find the array structure to change/set the value for a Node Reference field. I've looked around and tried every permutation I can find, including dozens of guesses at what the array structure should look like without any success. Any help would be appreciated.

  • Could you provide some code? – Neograph734 Jul 6 '17 at 14:54
  • @Neograph734 of what? every permutation I've tried, I didn't track it. I need the array structure to assign a node value to a node reference field – Geoff Jul 6 '17 at 15:12
  • Looking at the code I guess that should be an array of nids, like this: array(array('nid' => 1)) (single value field) or array(array('nid' => 1), array('nid' => 2)) (multi value). – Neograph734 Jul 6 '17 at 15:19
  • Node Reference as in provided by the obsolete References module? Or an Entity Reference? – Clive Jul 6 '17 at 15:29
  • it should be an array of nodes, but the exact structure eludes me: for example where a text field would be something like $form['field_name'][LANGUAGE_NONE][0]['value']['#value'] = 'abc', I don't know the corresponding structure and relevant names for a reference field – Geoff Jul 6 '17 at 15:30
0

With Clive's help, I was able to figure out the array structure I needed. To set the node ID for a Node Reference field (References Module) in hook_form_alter, this structure worked for me:

$form['field_name'][LANGUAGE_NONE]['#value'][0] = {node id}

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.