I'm building a custom field widget (may end up being contrib) and the field widget form has two inputs. I'm wondering if it is possible to know what the field name will be for use in #states
?
I wondered if it is possible to do something like ':input[name*="input_machine_name"]'
to see in the name contains that but it doesn't seem to work (although that is a valid jQuery selector otherwise).
I then tried to figure out how to derive the name from other values, possibly within hook_field_widget_WIDGET_TYPE_form_alter() but couldn't work out how to do it as the field is most likely to be used in a paragraph so could be deeply buried.
Any tips?
I've figured it out:
in
hook_field_widget_form()
:Also, turns out I can use
:input[name*="[input_machine_name]"]
; something else was wrong in my code before.For reference you can use:
:input[name*="[input_machine_name]"]
:input[name$="[input_machine_name]"]
I'll stick with the more precise definition to avoid targeting a similarly named property.
Not applicable to my example but the following would search for a string at the beginning of the field name:
:input[name^="search_string"]