How to set default value on Nodereference autocomplete field
This is how I set default value on Nodereference autocomplete field in an after build function. First set an afterbuild function in form alter hook, then in the after build function set the value. In this case I have added two values in the path calling for the form, node/add/project/[Parent nid]/[Parent name]
<?php
function mymodule_project_after_build($form, &$form_state) {
Of all these options to set value or default_value:
//$form['#node']->field_p_client[0]['nid'] = arg(3);
//$form['field_p_client'][0]['#default_value']['nid'] = arg(3);
//$form['field_p_client'][0]['#value']['nid'] = arg(4).' [nid:'.arg(3).']';
//$form['field_p_client'][0]['nid']['#default_value']['nid'] = arg(4).' [nid:'.arg(3).']';
//$form['field_p_client'][0]['nid']['#value']['nid'] = arg(4).' [nid:'.arg(3).']';
//$form['field_p_client'][0]['nid']['nid']['#default_value'] = arg(4).' [nid:'.arg(3).']';
...this one was the only needed:
$form['field_p_client'][0]['nid']['nid']['#value'] = arg(4).' [nid:'.arg(3).']';
return $form;
}
?>
With this code in form_alter you can pre set value to a CCK field and hide it in the same action
<?php
function yourmodule_form_alter(&$form, $form_state, $form_id) {
switch ($form_id) {
case 'your_form':
$form['yourfield'] = array(
'#type' => 'value',
'#value' => array( '0' => array('nid' => 'your referenced node id')),
break;
}
}
?>
http://drupal.org/node/564936#comment-4967216
Knowledge keywords: