Boolean formatter behavior is unexpected

Created on 11 February 2025, 4 months ago

Problem/Motivation

If a field is a boolean field, it should have a default fallback of FALSE.
When the filed is empty is shows as such. An end-user expects this to be X or V.

Steps to reproduce

Add a boolean field to a view and choose one of the formatters.

Proposed resolution

Default behavior of the bool formatter should be: if value empty => FALSE en if not => TRUE
and show the correct output of the formatter in my example X or V.

Remarks

This might have a good reason from a developer standpoint, but is confusing for an end-user.

🐛 Bug report
Status

Active

Version

10.4

Component

field system

Created by

🇧🇪Belgium jorisclaes

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @jorisclaes
  • 🇳🇿New Zealand quietone

    Changes are made on on 11.x (our main development branch) first, and are then back ported as needed according to the Core change policies .
    Changing tags per Issue tags field and Issue tags -- special tags

  • Hey,

    I have implemented the fix to ensure the Boolean field formatter properly displays FALSE when the value is empty. Previously, empty boolean fields were not rendered, which caused confusion for end users expecting a clear ✔ or ✖ (or any other format) output.

    Changes Made:

    • Updated BooleanFormatter.php to handle NULL values by defaulting them to FALSE.
    • Ensured that empty boolean fields are now correctly displayed in Views or any other place too, where the field is is used.
    • Refactored the logic to prevent skipped fields and improve code readability.

    Commit: Link

    Please review and test the patch to confirm expected behavior.
    Let me know if any improvements are needed.

    Thanks!

  • Pipeline finished with Success
    4 months ago
    Total: 443s
    #422071
  • We need the code changes in the form of a merge request to be reviewed. Thank you.

  • 🇧🇪Belgium jorisclaes

    My merge request was closed.

    @anish.ir
    What is wrong with returning before the foreach ?
    The foreach is skipped when the field is empty, so the variable elements will always be empty.

        if ($items->isEmpty()) {
          return [['#markup' => $formats[$format][1]]];
        }
    

    This I get and is and is an good improvement:
    $value = !empty($item->value) ? (bool) $item->value : FALSE;

  • Hey @jorisclaes,

    Thanks for the clarification!

    I initially removed the $items->isEmpty() check because I added a fallback at the end:
    return !empty($elements) ? $elements : [['#markup' => $formats[$format][1]]];

    However, I now see that keeping the early return makes the logic clearer and more efficient, since it avoids even entering the loop when there are no field values.
    I'll restore the $items->isEmpty() check and update the MR. Thanks for the feedback!

    Also, while raising the MR against 11.x, it reflected around 1236 commits to be merged to the 11.x branch, so I think the 3505775-boolean-formatter-behavior branch needs a rebase to sync with the latest 11.x.

    Please let me know, what's best here, to proceed further.
    Thanks!

  • 🇺🇸United States smustgrave

    have not reviewed but #6 is correct.

    Also if not already included but test coverage will be required.

  • Pipeline finished with Success
    4 months ago
    Total: 782s
    #423967
  • 🇧🇪Belgium jorisclaes

    Merge request is recreated

  • Hey,

    The MR with the required changes have been raised. Please have a look.
    Thanks !

  • 🇺🇸United States smustgrave

    Appears to be missing test coverage

  • Pipeline finished with Failed
    4 months ago
    Total: 533s
    #426979
  • 🇧🇪Belgium jorisclaes

    @13

    I don't have much experience with writing test for Drupal, if I forgot something plz let me know.

  • 🇺🇸United States smustgrave

    Thanks, left some small comments on the MR

    If you are another contributor eager to jump in, please allow the previous poster at least 48 hours to respond to feedback first, so they have the opportunity to finish what they started!

  • Pipeline finished with Success
    3 months ago
    Total: 587s
    #441161
  • 🇧🇪Belgium jorisclaes

    Thanks for the feedback @smustgrave,
    I reverted out of scope changes

    And added a comment that it is a fallback with FALSE as default value.

  • 🇺🇸United States smustgrave

    Feedback appears to be addressed

  • 🇬🇧United Kingdom catch

    One question/nit on the MR.

  • Status changed to Needs work about 1 month ago
  • First commit to issue fork.
  • Pipeline finished with Failed
    about 1 month ago
    Total: 90s
    #482000
  • Pipeline finished with Failed
    about 1 month ago
    Total: 154s
    #482003
  • Pipeline finished with Success
    about 1 month ago
    Total: 514s
    #482004
  • 🇮🇳India shalini_jha

    I have checked the issue and applied fixes based on the MR feedback. I updated the code to cast to bool directly, as this handles both true and false cases. There is no need for a ternary operator in this situation. I have reverified the test, and it is passing as expected.

    Additionally, I encountered linting failures for the existing code. I have addressed these issues, and the code now passes the linting checks. I am moving this back to Needs Review. Kindly review.

  • 🇺🇸United States smustgrave

    Feedback appears to be addressed.

Production build 0.71.5 2024