Conditional form states in hook_form_alter() I have a node with a field, whose widget uses select boxes and multiple choices are possible. The values are coming from a taxonomy entity reference field. There is another field, a text field, which needs to be hidden.
/**
 * Implements hook_form_alter().
 */
function mijn_condities_form_alter(&amp;$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  if($form_id == 'node_bedrijf_form' || $form_id == 'node_bedrijf_edit_form'){
    $form['field_afstand']['#states'] = array(
      'visible' =&gt; array(
        ':input[name=&quot;field_openbaar_vervoer&quot;]' =&gt; array('value' =&gt; '236'),
      )
      );
     
  }
}

A select box should be added according their choice. For example, when they choose bus or tram, there should be a field to enter the distance in meters. When they choose airport, the range must be in kilometers.
The field isn't hiding.
I already tried to make the field invisible, but this doesn't work.
Does anyone know what is wrong?
This is a little bit tricky the Form API #states works with jQuery selectors if your filed field_openbaar_vervoer coming from a taxonomy entity reference.
So the input generated HTML will be something like (example from tags fields)
&lt;input data-drupal-selector=&quot;edit-field-tags-3&quot; aria-describedby=&quot;edit-field-tags--description&quot; type=&quot;checkbox&quot; id=&quot;edit-field-tags-3&quot; name=&quot;field_tags[3]&quot; value=&quot;3&quot; class=&quot;form-checkbox&quot;&gt;

So to target all those inputs you should use Starts With Selector in your code and it will work:
$form['field_afstand']['#states'] = [
  'visible' =&gt; [
    ':input[name^=&quot;field_openbaar_vervoer&quot;]' =&gt; ['value' =&gt; '236'],
  ],
];