On one of my content types, I have a node reference field that I would like prepopulated with a node based on what's in the URL. I'm currently getting the node ID out of the URL, and populating the node reference field's #default_value with that nid, which works great for select lists or radio button widgets.
However, for better usability/performance, I'd like to use an autocomplete widget, but I can't seem to populate the #default_value appropriately - the field is empty.
Here's approximately how I'm trying to do this based on an nid:
function fn_notes_form_article_node_form_alter(&$form, &$form_state, $form_id) {
// If there's a list nid in the URL, store it (which prevents errors if the
// Upload button is clicked).
if ($list_node = node_load(arg(3)) and $list_node->type == 'list') {
$form_state['storage']['list_node'] = $list_node;
}
// If list node is present, prepopulate the list reference field.
if (!empty($form_state['storage']['list_node'])) {
$form['field_article_list_reference']['und']['#default_value'] = $form_state['storage']['list_node']->nid;
}
}
This is not working for autocomplete, as I said above. It seems that, for autocomplete, the #default_value might need to be populated in some other way, as the field is built differently. Does anyone know an easy way to get it filled in correctly?
Comments
Comment #1
Fidelix CreditAttribution: Fidelix commentedAw... I'm needing this too..
Comment #2
thirdender CreditAttribution: thirdender commentedI had some success with the latest version of References and the following code. I used the Devel module's dpm() function to examine the form and get the path I used for the #default_value value. I'm not sure this is the correct or best way to set the #default_value, so if someone could comment that'd be great.