I have 2 content types: registration (R) and class (C). R has an entity reference field which is referencing to C using the "Views: Filter by an entity reference view" mode. The entity reference field must use the select list widget. User account also has an entity reference field field_default_class which is referencing to C. Because of business logic, the field_default_class's value never exists in the entity reference view's filtered result which is used in the R node.
Now if an authenticated user adds or edits a node of R, I need to check if current user's field_default_class is set or not. If set, I need to pragmatically set the R's entity reference nodes' select list to the user's field_default_class's value, then disable the select list for disallowing user to change the value.
I guess it's difficult to set a select list's option value which is not in the view's filtered results. Or maybe my codes as below have errors. But I don't know where is wrong :-(
/**
* Implements hook_form_alter().
*/
function sisucustom_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'registration_node_form') {
global $user;
$account = user_load($user->uid);
if (isset($account->field_default_class['und'][0]['target_id'])) {
$entity_node = node_load($account->field_default_class['und'][0]['target_id']);
$options = array($entity_node->nid => $entity_node->title);
$form['field_register_for_class'][LANGUAGE_NONE][0]['target_id']['#options'] = $options;
$form['field_register_for_class'][LANGUAGE_NONE][0]['target_id']['#default_value'] = $entity_node->nid;
$form['field_register_for_class']['LANGUAGE_NONE']['#attributes']['#disabled'] = TRUE;
}
}
}
Or, is it possible to change the associated view to the entity reference field in node R? Because I can create another entity reference view which filters out only the one C which equals the current user's field_default_class's value. If it is, how to do it (in which hook)?
PS: I heavily use conditional fields module in node R. So maybe there are some pits against my goal. How to avoid those?