Skip to content

Commit

Permalink
When editing a custom field, load the existing values before copyValu…
Browse files Browse the repository at this point in the history
…es()
  • Loading branch information
michaelmcandrew committed May 31, 2018
1 parent 95eb360 commit 0970701
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
22 changes: 14 additions & 8 deletions CRM/Core/BAO/CustomField.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,14 +273,20 @@ public static function create(&$params) {
}

$customField = new CRM_Core_DAO_CustomField();
$customField->copyValues($params);
$customField->is_required = CRM_Utils_Array::value('is_required', $params, FALSE);
$customField->is_searchable = CRM_Utils_Array::value('is_searchable', $params, FALSE);
$customField->in_selector = CRM_Utils_Array::value('in_selector', $params, FALSE);
$customField->is_search_range = CRM_Utils_Array::value('is_search_range', $params, FALSE);
//CRM-15792 - Custom field gets disabled if is_active not set
$customField->is_active = CRM_Utils_Array::value('is_active', $params, TRUE);
$customField->is_view = CRM_Utils_Array::value('is_view', $params, FALSE);
if ($op == 'edit') {
$customField = CRM_Core_DAO_CustomField::findById($params['id']);
$customField->copyValues($params);
}
else {
$customField->copyValues($params);
$customField->is_required = CRM_Utils_Array::value('is_required', $params, FALSE);
$customField->is_searchable = CRM_Utils_Array::value('is_searchable', $params, FALSE);
$customField->in_selector = CRM_Utils_Array::value('in_selector', $params, FALSE);
$customField->is_search_range = CRM_Utils_Array::value('is_search_range', $params, FALSE);
//CRM-15792 - Custom field gets disabled if is_active not set
$customField->is_active = CRM_Utils_Array::value('is_active', $params, TRUE);
$customField->is_view = CRM_Utils_Array::value('is_view', $params, FALSE);
}
$customField->save();

// make sure all values are present in the object for further processing
Expand Down
27 changes: 27 additions & 0 deletions tests/phpunit/api/v3/CustomFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -569,4 +569,31 @@ public function getCustomFieldKeys($getFieldsResult) {
return $r;
}

/**
* This test is designed to ensure that when a custom field is updated via the
* API, params that are not supplied do not revert to the defaults. This was
* happening with, for example, is_searchable
*/
public function testDisableSearchableContactReferenceField() {
$customGroup = $this->customGroupCreate(array(
'name' => 'testCustomGroup',
'title' => 'testCustomGroup',
'extends' => 'Individual',
));
$params = array(
'name' => 'testCustomField',
'label' => 'testCustomField',
'custom_group_id' => 'testCustomGroup',
'data_type' => 'ContactReference',
'html_type' => 'Autocomplete-Select',
'is_searchable' => '1',
);
$result = $this->callAPISuccess('CustomField', 'create', $params);
$params = [
'id' => $result['id'],
'is_active' => 0,
];
$result = $this->callAPISuccess('CustomField', 'create', $params);
}

}

0 comments on commit 0970701

Please sign in to comment.