The module is designed for setting up Form API #States over configuration.

Installation

Install the module as you would normally install a contributed Drupal module. Visit https://www.drupal.org/node/1897420 for further information.

Configuration

  1. Go to Form States Configurations page (path /admin/structure/form-states-config).
  2. Hit 'Add Form States config' link.
  3. In 'Form ID' field put ID of a form which requires form states adjustments.
  4. In 'Form States' field add form states.
  5. Hit 'Save' button.

Example

Let's imagine user account has "Contact settings" (contact) and "Locale settings" (timezone) fields. "Administrator" (administrator) user role is only user role for which "Contact settings" and "Locale settings" fields should be shown on User Registration form. Create new Form States Configuration with next values:
- Form ID: user_register_form
- Form States:

contact:
  visible:
    :input[name="roles[administrator]"]:
      - checked: TRUE

timezone:
  visible:
    :input[name="roles[administrator]"]:
      - checked: TRUE

For more examples visit https://www.drupal.org/docs/drupal-apis/form-api/conditional-form-fields.

Notes:

1. Value for "Form States" must have a valid YAML format.
2. If it requires to add #states to nested array, for example $form['field_region']['widget']['#states'] then use field_region.widget.
3. Created configurations are exportable. Use `drush cim` to do that. Each of configuration will be exported to the config directory as form_states_config.form_states_config.[form_id].yml files.

References:

- https://www.drupal.org/docs/drupal-apis/form-api/conditional-form-fields

Supporting organizations: 

Project information

Releases