When creating a Content Type, one of your options is to create a field with a type: ‘Node reference’. Node references are a bit different than normal fields, in that they use other Content Type’s values to populate the field values.
This difference leads to some issues when trying to alter the Node Reference field on a Content Type form. What ends up happening, is that we do not have the ability to add/remove/alter values using the hook_form_alter() function directly. We end up needing a helper function for this. The code below is based on a select field:
function myModule_form_alter(&$form, &$form_state){
if (arg(0) == 'node' && $form['form_id']['#value'] == 'myContentType_node_form') {
// Replace our node reference field with the options we want using our helper
$form['field_location']['#pre_render'] = array('helper_change_options');
}
}
function helper_change_options($element) {
$our_new_options = array(‘key’=>’value’, ‘key2’=>’value2’);
// insert our new values into the element (which is a select field, options)
$element ['nid']['nid']['#options'] = $our_new_options; return $element;
}
Please note that whatever options you change or add, those node key values DO need to exist in the Node Reference’s Content Type. Otherwise you are going to run into some problems.