field_visibility_conditions
A Drupal module that allows you to control the visibility of fields based on configurable conditions.
Overview
Field Visibility Conditions provides a flexible way to control when fields are displayed or hidden on your Drupal site. This module extends Drupal's field configuration to include conditional visibility rules, making it easy to show or hide fields based on various conditions.
But why?
The benefit of this approach is for multi-sites, or shared codebase platforms, that share configuration across multiple sites but want some flexibility in control of what fields are present on entity forms. For example, if there are two sites sharing the same codebase, Site A and Site B, and only Site B needs a particular field on an entity type bundle, then using third party settings we are able to configure the visibility of that field based on a condition - this could be one provided by core, the contrib space, or something custom. This way both sites can still share the same configuration, as the third party settings are stored in config, but the conditions are evaluated per-site.
Requirements
- Drupal 10.x or 11.x
- PHP 8.1 or higher
Installation
1. Download the module:
You can download the module using Composer:
composer require drupal/field_visibility_conditions
2. Enable the module:
After downloading, enable the module using Drush or the Drupal admin interface:
drush en field_visibility_conditions
Or, enable it through the admin interface at /admin/modules.
Configuration
The module offers a settings page to configure which conditions are available for use with restricting field visibility.
Usage
1. Navigate to the field configuration for any field where you want to set visibility conditions
2. Look for the "Field Visibility" section
3. Add one or more conditions that determine when the field should be visible
4. Configure the condition parameters as needed
5. Save the field configuration
Extending the module
Developers can alter the available conditions using the provided hook:
/**
* Implements hook_field_visibility_conditions_available_conditions_alter().
*/
function my_module_field_visibility_conditions_available_conditions_alter(array &$conditions, FormStateInterface $form_state, ?string $form_id): void {
// Remove specific conditions
$conditions_to_remove = [
'language',
'request_path',
];
$conditions = array_diff_key($conditions, array_flip($conditions_to_remove));
}
Contributing
Contributions are welcome! If you would like to contribute to this module, please follow these steps:
1. Open an issue on the issue tracker
2. Create a Merge Request or add a patch
3. Mark your issue as Needs Review.
License
This module is licensed under the GPL-2.0+ license.