This module allows you to configure pairs of flags so that only one flag in the pair can be set on an entity at a time. For example, if you have a "like" and "dislike" flag on a post, setting one will automatically remove the other.
This module enforces mutual exclusion between configured groups of (Flag module) flags on the same entity. When a user sets one flag in a group of flags on an entity, the remaining flags are unset.
Features
The Flag module does not natively enforce mutual exclusion among flags. This module adds that capability by listening to Flag's ENTITY_FLAGGED event and automatically unsets the counterpart flags when one flag in a configured group is set.
Common use case:
- like/dislike reactions (group of two flags) where a user can hold only one at a time. Any number of groups can be configured, across any entity types and any flags that Flag supports.
- multiple choice settings (groups of multiple flags) where a user can select only one selection at a time. Up to 10 flags can be configured in a group. Common examples might be: subscription levels on a member plan, multiple choice options on an exam, user polls
Post-installation instructions are summarized below, additional documentation is contained in the module’s README file.
Breaking change (Release 1.1.x) : prior to this release, the module used a “pairs" model (exactly two flags per rule, stored under a pairs config key). This release replaces pairs entirely with a groups model (2-10 flags per rule, stored under a groups config key). Existing pair configuration is automatically migrated to groups the next time you run database updates — see "Upgrading from 1.0” in the README.
Post-Installation
1. Navigate to Administration → Structure → Flags (/admin/structure/flags) and add the flags that are intended to work as a mutually exclusive group. Make a note of their names, and the entity type to which they're configured to apply.
2. Navigate to Configuration → Content authoring → Flag mutually exclusive
(/admin/config/content/flag-mutually-exclusive) and click Add group.
Configuration is exported as part of flag_mutually_exclusive.settings and
is included in drush config:export / drush config:import deployments.
Configuration is exported as part of flag_mutually_exclusive.settings`and
is included in drush config:export / drush config:import deployments.
Additional Requirements
- Drupal ^11.3
- flag:flag (>=5.0.0)
Similar projects
Rules can be used to configure this functionality. However Rules is heavyweight, this module targets a specific set of functionality that managed Flags on entities in a specific manner, and provides that functionality without the overhead of Rules.
Supporting this Module
This module is supported by Lisa Harrison (lisa.rae), an individual contributor and long-time member of the Drupal community.