Saving checkbox custom fields via APIv4 fails for custom/dynamic entities
Steps to reproduce
- For a custom/dynamic entity type (e.g. using the Entity Construction Kit), add a custom field with HTML type
CheckBox
- Try to create/save an entity with that field with APIv4, e.g. using the ECK Afform forms (whether the field has a value is irrelevant)
Expected behavior
Entity saved, field has selected (or no) value
Actual behavior
Entity not saving, error is Error: Class name must be a valid object or a string in civicrm_api3_generic_getoptions() (line 442 of /path/to/civicrm/civicrm-core/api/v3/Generic.php).
Technical details
-
DAOActionTrait
calls the global (APIv3) functionformatCheckBoxField()
for fields with an HTML type ofCheckBox
here - That function calls APIv3
getoptions
with the given entity (Eck_*
) - APIv3 "normalizes" entity names, stripping the
_
which results in the APIv3 entity beingEck*
- Resolving the entity to a DAO class fails at the latest in
_civicrm_api3_get_DAO()
due to the "normalized" entity name - Finally, when calling
$dao::buildOptions()
onnull
, the error occurs
Proposed solution
As commented in DAOActionTrait
for the call of formatCheckBoxField()
: this function should be part of a class
- and re-implemented using APIv4 getFields
with loadOptions=TRUE
for the given custom_field_id
.