Score:0

State API doesn't work on new paragraph fields

kr flag

I have programmatically added #states into fields in a paragraph works fine for existing fields in a form. But on clicking add more button on paragraph to add more fields the hidden fields show up. I tried this Patch but it didn't work for me.

I have used this hook to alter the field to add #states.

function mymodule_field_widget_entity_reference_paragraphs_form_alter(&$element, &$form_state, $context) {
  $element['subform']['field_title']['#states'] = [
    'invisible' => [
      '#edit-parent-field-0-subform-parent-field-content-'.$element['#delta'].'-subform-target-field-wrapper select' => ['value' => 'value']
    ]  
  ];
}
ru flag
This patch is no longer necessary, this is already included in stable. If the code doesn't work, one of your selectors is incorrect.
arpitk avatar
kr flag
But the same code works perfectly fine on initial page load. only fails when we try to add a new paragraph.
arpitk avatar
kr flag
I guess when we add a new paragraph the form rebuilds but fails to implement the #states. I tried to print a variable into this hook and it worked to check if this hook is called on ajax call or not. But yes it is been called still the #states code doesnt work.
ru flag
Just a blind guess, but if it works intially and not for newly added paragraphs, then the zero in `edit-parent-field-0-subform-parent-field-content` looks suspicious
arpitk avatar
kr flag
Thanks for this comment. I tried different approach changed the selector to select[name="field_parent[0][subform][field_parent_content][0][subform][field_target_field]"]. And its working perfectly fine now.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.