Selected yet disabled individual options from checkboxes widget don't persist through save

Created on 8 March 2022, about 3 years ago
Updated 5 December 2023, over 1 year ago

Problem/Motivation

It is possible to programatically disable individual options on a checkboxes widget with the intention to show the selected value to users but disallow deselection, but if you do so the previously stored value will be removed despite having been show to the user as selected.

This is split out from #2911473: Selected yet disabled individual options from checkboxes element don't persist through save which addresses this more generally at the FAPI level and should be fixed first.

Steps to reproduce

  1. Add a field to article, as follows:
  • Field type: boolean
  • Field label: My Field
  • Save and continue
  • Set number of values to unlimited
  • Save field settings
  • Leave all settings, and click Save settings
  • Configure the Manage form display to use the widget "Checkboxes/radio buttons".
  • Add this line of code as the last line in book_form_node_form_alter
  • $form['field_my_field']['widget'][0]['#disabled'] = TRUE;
  • Enable the book module
  • Add an article, view the form -- see that the first option is disabled but selected
  • Submit the node form.
  • Edit the new article node -- see that the first option is disabled but NOT selected
  • Proposed resolution

    To be determined.

    Remaining tasks

    Diagnose.
    Fix.
    Add test.

    User interface changes

    None.

    API changes

    None.

    Data model changes

    None.

    🐛 Bug report
    Status

    Active

    Version

    11.0 🔥

    Component
    Field 

    Last updated about 12 hours ago

    Created by

    🇬🇧United Kingdom jonathanshaw Stroud, UK

    Live updates comments and jobs are added and updated live.
    • Needs tests

      The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

    Sign in to follow issues

    Comments & Activities

    Not all content is available!

    It's likely this issue predates Contrib.social: some issue and comment data are missing.

    • 🇬🇧United Kingdom catch

      Would be good to add some test coverage here, both to validate a fix and to show that the parent issue didn't help.

    • 🇫🇮Finland Tuuuukka

      When you submit the checkboxes widget with only disabled options checked, the value callback function gets nothing for input. If you check/select an enabled option too, the disabled ones keep their values also.

      The input is checked here: https://git.drupalcode.org/project/drupal/-/blob/11.x/core/lib/Drupal/Co.... As stated, with only disabled options checked, the $input variable is empty, so it defaults to using $element['#default_value'], but that's empty too, so it all ends up returning an empty array.

    Production build 0.71.5 2024