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.

  • 1
    Not really an answer to your question, but there is an alternative, no-code solution for this: With views module, create a view of taxonomy terms. Filter that view as required (in your case most likely a contextual filter on the current user). As display plugin for that view select "entity reference". Then on node field settings, select "Views: Filter by an entity reference view" as "Reference method".
    – Hudri
    Apr 5, 2023 at 15:56
  • Thanks Hudri let me try that. Apr 5, 2023 at 16:16

1 Answer 1


Thanks to @Hudri suggestion. I am able to achieve it without code using views module.

I followed the guide from How to Use Entity Reference Views in Drupal 8 - OsTraining and created a contextual filter to filter by current logged in user.

The important thing here is define Configure contextual filter When the filter value is NOT available.

View settings

View Contextual Filter Settings

Node Field Channel


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.