Problem/Motivation

The Primary Entity Reference Options Buttons field widget has an undesirable behavior where users can select a primary entity reference using the radio button regardless of whether the corresponding checkbox is selected. This creates an inconsistent state where an unchecked item can be marked as primary.

Steps to reproduce

  1. Create a Primary Entity Reference field with the Options Buttons widget
  2. Configure the field to display checkboxes with radio buttons for primary selection
  3. Attempt to select a radio button for an item without first checking its checkbox
  4. Observe that the radio button can be selected even though the checkbox is unchecked

Proposed resolution

Update the widget behavior to enforce the following rules:

  • Auto-select first checked item as primary: When no items are selected and a user checks the first checkbox, that item should automatically become the primary
  • Disable radio buttons for unchecked items: Radio buttons should be disabled unless their corresponding checkbox is checked
  • Clear primary on uncheck: If an item is unchecked and it was the primary, it should no longer be the primary, and no primary should be selected
  • Single item auto-primary: If only one checkbox is selected, it should automatically be set as the primary without requiring explicit radio button selection
  • Form submission validation: On form submit, if multiple checkboxes are selected but no primary is chosen, display an error message requiring the user to select a primary. If only one checkbox is selected, automatically set it as the primary and allow submission
  • JavaScript validation: Implement client-side JavaScript to enforce these rules and provide immediate feedback

Remaining tasks

  • Update JavaScript in js/primary-selection.js to implement the new behavior
  • Add form validation logic to check for primary selection on submit
  • Implement auto-primary assignment for single-item selections
  • Update CSS in css/primary-selection.css if needed for disabled state styling
  • Add/update unit tests for the widget behavior including validation scenarios
  • Test manually with various scenarios (single item, multiple items, checking/unchecking, form submission with no primary)
  • Update documentation if necessary

User interface changes

  • Radio buttons will be disabled (grayed out) when their associated checkbox is not checked
  • The first checkbox that is checked will automatically have its radio button selected
  • Unchecking a checkbox will automatically disable its radio button and deselect it if it was the primary
  • If only one checkbox is selected, its radio button will be automatically selected as the primary
  • On form submission, if multiple checkboxes are selected but no primary is chosen, an error message will be displayed: "You must select a primary entity reference when multiple items are selected"

API changes

None

Data model changes

None

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

bluegeek9 created an issue. See original summary.

  • bluegeek9 committed 6559f003 on 1.0.x
    feat: #3570148 Options Widget behavior: checkboxes
    
bluegeek9’s picture

Status: Active » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.