- 🇫🇷France mably
Thanks @jhuhta, your small comment saved my day 🙂
When using the States API in a paragraph, you have to use a different syntax for the :required filter than the invisible filter for example.
You have to add that
['widget'][0 or whatever delta you have]['value']
before['#states'] = [...].
.Here is the final working code of my hook:
/** * Implements hook_field_widget_single_element_form_alter(). */ function my_module_field_widget_single_element_form_alter(&$element, FormStateInterface $form_state, $context) { if (isset($element['#paragraph_type']) && $element['#paragraph_type'] === 'my_paragraph_type') { $parents_array = $element['subform']['#parents']; $parents = array_shift($parents_array) . '[' . implode('][', $parents_array) . ']'; $element['subform']['field_title']['#states'] = [ 'invisible' => [ ':input[name="' .$parents . '[field_display_mode][0][value]"]' => ['value' => 'three'], ] ]; $element['subform']['field_title']['widget']['0']['value']['#states'] = [ 'required' => [ ':input[name="' .$parents . '[field_display_mode][0][value]"]' => ['value' => 'two'], ], ]; } }