Skip to main content
drupalreleases

Use direct release data with your AI assistant.

Learn more

Form API States configs

Security covered Under active development

This module allows you to configure conditional logic for form fields using the Form API's #states property. You can set up rules to show or hide form elements based on other form values or user roles.

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

Ask your assistant about Form API States configs

Check compatibility with your Drupal and PHP version, maintenance and security coverage, from current release data. How it works

Depends on

Dependencies of the latest stable release

No dependencies recorded for this project.

Required by

Tracked projects that depend on this one

No tracked projects depend on this one yet.

Activity

Tracked releases
2
Tracked since
Apr 2023
Latest release
2 years ago
Releases (12 mo)
0
Maintenance
Dormant

Releases

Version Type Core PHP Release date
1.0.0-beta1 Pre-release 10 Dec 2, 2023
1.0.0-alpha1 Pre-release 9 Apr 13, 2023