Currently, list_field_is_empty()
uses the following conditional to test whether a field is empty:
<?php
if (empty($item['value']) && (string) $item['value'] !== '0')
?>
In other words, if the empty()
function returns TRUE and the string value of the field is also not zero, then it is empty. This seems fine for most list fields, but causes big problems when writing code for Boolean fields. One would reasonably expect this function to return TRUE for a value of 0, which in the context of a single on/off checkbox, is empty. However, this function will always return FALSE for Booleans, whether you pass it 0 or 1.
One place where this causes problems is with field collections. Suppose you have a field collection that consists of two required fields and one optional Boolean (a single on/off checkbox). The field collection module uses the emptiness of the collection to decide whether to enforce validation of required fields. But because list_field_is_empty()
always returns FALSE for single on/off checkboxes, the collection can never be considered empty.
The attached patch adds a check to see if we're dealing with a list_boolean field and, if so, considers 0 to be empty.
Attachment | Size | Status | Test result | Operations |
---|---|---|---|---|
field-return_true_for_booleans.patch | 616 bytes | Idle | FAILED: [[SimpleTest]]: [MySQL] 40,345 pass(es), 2 fail(s), and 1 exception(s). | View details | Re-test |