Thanks @AyeshK and @Beebee for pointing me in the right direction. Here is how I finally got hold of the selected value and displayed it via an ['#ajax']['callback']
I attached to the node_reference
select list. From there on it's quite simple to modify the markup to more advanced needs like displaying a preview of a node or a field.
In the end you might want to add a second wrapper with the same ID to hold the default value when editing an existing node.
/**
* Implements hook_form_alter().
*/
function MY_MODULE_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'MY_FORM') {
// add an Ajax callback to the select list and define a wrapper the action should happen in
$form['field_node_reference'][LANGUAGE_NONE]['#ajax']['callback'] = '_MY_MODULE_ajax_callback';
$form['field_node_reference'][LANGUAGE_NONE]['#ajax']['wrapper'] = 'MY-ID';
// create a wrapper
$form['MY_WRAPPER'] = array(
'#type' => 'markup',
'#prefix' => '<div id="MY-ID">',
'#suffix' => '</div>',
'#weight' => 3,
);
// do some magic
if (isset($form_state['values']['field_node_reference'][LANGUAGE_NONE][0]['nid']) && $form_state['values']['field_node_reference'][LANGUAGE_NONE][0]['nid'] != '') {
$nid = $form_state['values']['field_node_reference'][LANGUAGE_NONE][0]['nid'];
$form['MY_WRAPPER']['#markup'] = t('You selected node number: @nid', array('@nid' => $nid));
}
}
}
function _MY_MODULE_ajax_callback(&$form, &$form_state) {
// return the wrapper
return $form['MY_WRAPPER'];
}