diff --git a/api/v3/CustomValue.php b/api/v3/CustomValue.php index e53b39d88a70..3a855d21f310 100644 --- a/api/v3/CustomValue.php +++ b/api/v3/CustomValue.php @@ -149,7 +149,10 @@ function civicrm_api3_custom_value_get($params) { if (!empty(substr($id, 7))) { $returnVal = substr($id, 7); } - foreach ((array) $returnVal as $value) { + if (!is_array($returnVal)) { + $returnVal = explode(',', $returnVal); + } + foreach ($returnVal as $value) { list($c, $i) = CRM_Utils_System::explode('_', $value, 2); if ($c == 'custom' && is_numeric($i)) { $names['custom_' . $i] = 'custom_' . $i; diff --git a/tests/phpunit/api/v3/CustomValueTest.php b/tests/phpunit/api/v3/CustomValueTest.php index 133a9f812add..d50a684e53c3 100644 --- a/tests/phpunit/api/v3/CustomValueTest.php +++ b/tests/phpunit/api/v3/CustomValueTest.php @@ -552,4 +552,72 @@ public function testUpdateCustomGreetings() { } + /** + * Creates a multi-valued custom field set and creates a contact with mutliple values for it. + * + * @return array + */ + private function _testGetCustomValueMultiple() { + $fieldIDs = $this->CustomGroupMultipleCreateWithFields(); + $customFieldValues = []; + foreach ($fieldIDs['custom_field_id'] as $id) { + $customFieldValues["custom_{$id}"] = "field_{$id}_value_1"; + } + $this->assertNotEmpty($customFieldValues); + $contactParams = [ + 'first_name' => 'Jane', + 'last_name' => 'Doe', + 'contact_type' => 'Individual', + ]; + $contact = $this->callAPISuccess('Contact', 'create', array_merge($contactParams, $customFieldValues)); + foreach ($fieldIDs['custom_field_id'] as $id) { + $customFieldValues["custom_{$id}"] = "field_{$id}_value_2"; + } + $result = $this->callAPISuccess('Contact', 'create', array_merge(['id' => $contact['id']], $customFieldValues)); + return [ + $contact['id'], + $customFieldValues, + ]; + } + + /** + * Test that specific custom values can be retrieved while using return with comma separated values as genererated by the api explorer. + * ['return' => 'custom_1,custom_2'] + */ + public function testGetCustomValueReturnMultipleApiExplorer() { + list($cid, $customFieldValues) = $this->_testGetCustomValueMultiple(); + $result = $this->callAPISuccess('CustomValue', 'get', [ + 'return' => implode(',', array_keys($customFieldValues)), + 'entity_id' => $cid, + ]); + $this->assertEquals(count($customFieldValues), $result['count']); + } + + /** + * Test that specific custom values can be retrieved while using return with array style syntax. + * ['return => ['custom_1', 'custom_2']] + */ + public function testGetCustomValueReturnMultipleArray() { + list($cid, $customFieldValues) = $this->_testGetCustomValueMultiple(); + $result = $this->callAPISuccess('CustomValue', 'get', [ + 'return' => array_keys($customFieldValues), + 'entity_id' => $cid, + ]); + $this->assertEquals(count($customFieldValues), $result['count']); + } + + /** + * Test that specific custom values can be retrieved while using a list of return parameters. + * [['return.custom_1' => '1'], ['return.custom_2' => '1']] + */ + public function testGetCustomValueReturnMultipleList() { + list($cid, $customFieldValues) = $this->_testGetCustomValueMultiple(); + $returnArray = []; + foreach ($customFieldValues as $field => $value) { + $returnArray["return.{$field}"] = 1; + } + $result = $this->callAPISuccess('CustomValue', 'get', array_merge($returnArray, ['entity_id' => $cid])); + $this->assertEquals(count($customFieldValues), $result['count']); + } + }