Skip to content

Commit

Permalink
Merge pull request #22328 from colemanw/fixCustomNull
Browse files Browse the repository at this point in the history
APIv4 - Fix saving NULL as custom field value
  • Loading branch information
demeritcowboy authored Dec 27, 2021
2 parents ddee419 + 540c0ec commit af72c7c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 32 deletions.
65 changes: 33 additions & 32 deletions Civi/Api4/Generic/Traits/DAOActionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,43 +257,44 @@ protected function formatCustomParams(&$params, $entityId) {
continue;
}

// todo are we sure we don't want to allow setting to NULL? need to test
if (NULL !== $value) {
// Null and empty string are interchangeable as far as the custom bao understands
if (NULL === $value) {
$value = '';
}

if ($field['suffix']) {
$options = FormattingUtil::getPseudoconstantList($field, $name, $params, $this->getActionName());
$value = FormattingUtil::replacePseudoconstant($options, $value, TRUE);
}
if ($field['suffix']) {
$options = FormattingUtil::getPseudoconstantList($field, $name, $params, $this->getActionName());
$value = FormattingUtil::replacePseudoconstant($options, $value, TRUE);
}

if ($field['html_type'] === 'CheckBox') {
// this function should be part of a class
formatCheckBoxField($value, 'custom_' . $field['id'], $this->getEntityName());
}
if ($field['html_type'] === 'CheckBox') {
// this function should be part of a class
formatCheckBoxField($value, 'custom_' . $field['id'], $this->getEntityName());
}

// Match contact id to strings like "user_contact_id"
// FIXME handle arrays for multi-value contact reference fields, etc.
if ($field['data_type'] === 'ContactReference' && is_string($value) && !is_numeric($value)) {
// FIXME decouple from v3 API
require_once 'api/v3/utils.php';
$value = \_civicrm_api3_resolve_contactID($value);
if ('unknown-user' === $value) {
throw new \API_Exception("\"{$field['name']}\" \"{$value}\" cannot be resolved to a contact ID", 2002, ['error_field' => $field['name'], "type" => "integer"]);
}
// Match contact id to strings like "user_contact_id"
// FIXME handle arrays for multi-value contact reference fields, etc.
if ($field['data_type'] === 'ContactReference' && is_string($value) && !is_numeric($value)) {
// FIXME decouple from v3 API
require_once 'api/v3/utils.php';
$value = \_civicrm_api3_resolve_contactID($value);
if ('unknown-user' === $value) {
throw new \API_Exception("\"{$field['name']}\" \"{$value}\" cannot be resolved to a contact ID", 2002, ['error_field' => $field['name'], "type" => "integer"]);
}

\CRM_Core_BAO_CustomField::formatCustomField(
$field['id'],
$customParams,
$value,
$field['custom_group_id.extends'],
// todo check when this is needed
NULL,
$entityId,
FALSE,
$this->getCheckPermissions(),
TRUE
);
}

\CRM_Core_BAO_CustomField::formatCustomField(
$field['id'],
$customParams,
$value,
$field['custom_group_id.extends'],
// todo check when this is needed
NULL,
$entityId,
FALSE,
$this->getCheckPermissions(),
TRUE
);
}

$params['custom'] = $customParams ?: NULL;
Expand Down
12 changes: 12 additions & 0 deletions tests/phpunit/api/v4/Action/BasicCustomFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,18 @@ public function testWithSingleField(): void {
->first();

$this->assertEquals('Blue', $contact['MyIndividualFields.FavColor']);

// Try setting to null
Contact::update()
->addWhere('id', '=', $contactId)
->addValue('MyIndividualFields.FavColor', NULL)
->execute();
$contact = Contact::get(FALSE)
->addSelect('MyIndividualFields.FavColor')
->addWhere('id', '=', $contactId)
->execute()
->first();
$this->assertEquals(NULL, $contact['MyIndividualFields.FavColor']);
}

public function testWithTwoFields() {
Expand Down

0 comments on commit af72c7c

Please sign in to comment.