diff --git a/api/v3/CustomValue.php b/api/v3/CustomValue.php index 814e7faa6aad..8c45ece60114 100644 --- a/api/v3/CustomValue.php +++ b/api/v3/CustomValue.php @@ -363,14 +363,14 @@ function civicrm_api3_custom_value_gettree($params) { unset($field['customValue']); if (!empty($fieldInfo['customValue'])) { $field['value'] = CRM_Utils_Array::first($fieldInfo['customValue']); + if (!$toReturn['custom_value'] || in_array('display', $toReturn['custom_value'])) { + $field['value']['display'] = CRM_Core_BAO_CustomField::displayValue($field['value']['data'], $fieldInfo); + } foreach (array_keys($field['value']) as $key) { if ($toReturn['custom_value'] && !in_array($key, $toReturn['custom_value'])) { unset($field['value'][$key]); } } - if (!$toReturn['custom_value'] || in_array('display', $toReturn['custom_value'])) { - $field['value']['display'] = CRM_Core_BAO_CustomField::displayValue($field['value']['data'], $fieldInfo); - } } if (empty($params['sequential'])) { $result[$group['name']]['fields'][$fieldInfo['name']] = $field; diff --git a/tests/phpunit/api/v3/CustomValueTest.php b/tests/phpunit/api/v3/CustomValueTest.php index 3709ee86d1fc..4687f73121b2 100644 --- a/tests/phpunit/api/v3/CustomValueTest.php +++ b/tests/phpunit/api/v3/CustomValueTest.php @@ -428,8 +428,20 @@ public function testGettree() { ), )); $this->assertEquals(array('2', '3'), $tree['values']['TestGettree']['fields']['got_options']['value']['data']); + $this->assertEquals('Two, Three', $tree['values']['TestGettree']['fields']['got_options']['value']['display']); $this->assertEquals(array('id', 'fields'), array_keys($tree['values']['TestGettree'])); + // Ensure display values are returned even if data is not + $tree = $this->callAPISuccess('CustomValue', 'gettree', array( + 'entity_type' => 'Contact', + 'entity_id' => $contact, + 'return' => array( + 'custom_value.display', + ), + )); + $this->assertEquals('Two, Three', $tree['values']['TestGettree']['fields']['got_options']['value']['display']); + $this->assertFalse(isset($tree['values']['TestGettree']['fields']['got_options']['value']['data'])); + // Verify that custom set appears for individuals even who don't have any custom data $contact2 = $this->individualCreate(); $tree = $this->callAPISuccess('CustomValue', 'gettree', array('entity_type' => 'Contact', 'entity_id' => $contact2));