Score:0

Using #states on an inline paragraph form

ph flag

I have a field on a custom content type which is a reference to a paragraph type, using the Paragraph (EXPERIMENTAL) widget. I'm trying to modify that widget to show and hide a field based on another field on the paragraph. Here is my widget alter code:

$field = 'field_supporter_form_fields';
$id_field = 'field_primary_custom';
$delta = $element['#delta'];
$id = sprintf('select[name="%s[%d][subform][%s]"]', $field, $delta, $id_field);
$element['subform']['field_field_type']['widget']['#states'] = [ 
  'visible' => [
    $id => ['value' => 'custom'],
  ],
];
$element['subform']['field_field_type']['widget']['#title'] = 'Blah!';

I know that I'm targeting the correct widget because I see the field title change to "Blah!", but the visibility of the field doesn't change.

Here's what the relevant part of the $element array looks like after the alter:

[#parents] => Array
    (
        [0] => field_supporter_form_fields
        [1] => 0
        [2] => subform
        [3] => field_field_type
    )

[#states] => Array
    (
        [visible] => Array
            (
                [select[name="field_supporter_form_fields[0][subform][field_primary_custom]"]] => Array
                    (
                        [value] => custom
                    )

            )

    )

The dependant field field_primary_custom is a pair of radio buttons with values of either primary or custom. The name of the element on the page is field_supporter_form_fields[0][subform][field_primary_custom] which matches what I have in my identifier.

Why isn't the visibility changing?

Score:0
ph flag

Turns out I needed to use input instead of select. Not entirely sure why, but it works!

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.