Can anyone please let me how to alter entity reference field (taxonomies) field options in hook form alter,
Seems the below code doesn't work.
/**
* Implements hook_form_alter().
*/
function MY_MODULE_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if ($form_id == 'node_article_edit_form' || $form_id == 'node_article_add_form') {
$channel_options = [
'1' => 'Term1',
'2' => 'Term2',
'3' => 'Term3',
'4' => 'Term4',
'5' => 'Term5',
];
$form['field_channel'] = [
'#title' => 'Channels',
'#description' => 'Select Channel',
'#type' => 'select',
'#options' => $channel_options,
];
}
}
For context :
I have a field named as channels in node article which is entity reference to channel taxonomy.
I would like to limit the list of terms visible to current user to the channels to which they are assigned in their user profile.
I am trying to achieve it by altering the node edit form and replacing channels field values from the current user logged in.