Setting #states property for a field_collection item added with the add_more button This is a D8 question as the solution in Using #states with field collection doesn't work for D8.

Here are the solutions I have tried, but none of them was working:

1- Adding an after build function for the field_collection element

$form['my_fc_field']['#after_build'][] = 'my_fc_field_after_build';


=>The function is not called when the "add_more" button is pressed

2- Adding an after build function for the field_collection item element

$form['my_fc_field']['widget'][0]['fc_item_field']['#after_build'][] = 'fc_item_field_after_build';


=>The function is not called when the "add_more" button is pressed

3- Altering my field_collection widget

function artprod_field_widget_form_alter(&amp;$element, \Drupal\Core\Form\FormStateInterface $form_state, $context) {
  $field_definition = $context['items']-&gt;getFieldDefinition();
  if ($field_definition-&gt;getName() == 'my_fc_field') {
    $widgets_that_need_states = &amp;$element['my_fc_field']['widget'];
    $childs = Element::children($widgets_that_need_states);
    foreach ($childs as $child) {
      $widgets_that_need_states[$child]['value']['#states'] = array(
        'visible' =&gt; array(
          "select[data-drupal-selector=\"xxxx\"]" =&gt; array(
            'value' =&gt; 'yyy'
          )
        )
      );
    }
  }


=>The function is not called when the "add_more" button is pressed

Conclusion: with all of those 3 methods I could set the #states property to the already constructed element, but not for the added elements.
I could solve it myself...

The solution is to alter the field_collection widget. In the point 3 of my question, I have made a mistake in the code. Here is a generic code to handle this behavior for changing the visibility of a text field according to the value of a select field:

function your_module_field_widget_form_alter(&amp;$element, FormStateInterface $form_state, $context) {
  $fn = $context['items']-&gt;getFieldDefinition()
    -&gt;getName();
  if ($fn == 'your_field_collection')  {
    // Get the delta
    $delta = $element['#delta'];
    $element['your_fc_text_field']['widget'][0]['value']['#states'] = array(
      'visible' =&gt; array(
        "select[data-drupal-selector=\"edit-your-field-collection-$delta-your-fc-text-field\"]" =&gt; array(
          'value' =&gt; "your_value_when_visible"
        )
      )
    );
  }
}


Note 1: I used "data-drupal-selector" because this was the only jquery selector which was working for the "select" (select seems more tricky then other fields)

Note 2: Adding ":" before "select" like ":select[data..." is breaking the feature. I don't know why!

Note 3: take care of the $delta in the data-drupal-selector

Note 4: Use "Inspect element" of your browser to get the correct value of your "data-drupal-selector"