Missing "None" Option on Checkbox/Radio Display

Created on 25 July 2024, 4 months ago

Problem/Motivation

When I use the checkbox/radio format, the "- None -" option is often missing.

Steps to reproduce

I can't tell why some of my fields have it and some do not. They are all taxonomy reference fields with the exact same settings.

However, a fresh test reproduces the error for me.

Create a new entity reference field for a taxonomy vocabulary.
Change the field to "select or other" in the form display.
Change select list to checkboxes.

The "- None -" option is missing.

Proposed resolution

I have altered this code to check that the array contains no default items, rather than just if the array exists.

  protected static function addEmptyOption(array &$element) {
    if ((!isset($element['#no_empty_option']) || !$element['#no_empty_option']) && !$element['#multiple']) {
      if (!$element['#required'] && !count($element['#default_value']) > 0) {
        $element['select']['#options'] = ['' => t('- None -')] + $element['select']['#options'];
      }
      else {
        $element['select']['#value'] = [];
      }
    }
  }

This seems to work for me. I would welcome some thoughts on it or if this is an issue for anyone else.

I can also confirm this issue, however, I don't think it's 100% directly related to my issue:
https://www.drupal.org/project/select_or_other/issues/3260054 πŸ› - None - option for default value in field settings form is being removed Active

πŸ› Bug report
Status

Active

Version

4.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States xpersonas

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

Comments & Activities

Production build 0.71.5 2024