/** * Use this API to get existing custom values for an entity. * * @param array $params * Array specifying the entity_id. * Optionally include entity_type param, i.e. 'entity_type' => 'Activity' * If no entity_type is supplied, it will be determined based on the fields you request. * If no entity_type is supplied and no fields are specified, 'Contact' will be assumed. * Optionally include the desired custom data to be fetched (or else all custom data for this entity will be returned) * Example: 'entity_id' => 123, 'return.custom_6' => 1, 'return.custom_33' => 1 * If you do not know the ID, you may use group name : field name, for example 'return.foo_stuff:my_field' => 1 * * @throws API_Exception * @return array */ function civicrm_api3_custom_value_get($params) { $getParams = array('entityID' => $params['entity_id'], 'entityType' => CRM_Utils_Array::value('entity_table', $params, '')); if (strstr($getParams['entityType'], 'civicrm_')) { $getParams['entityType'] = ucfirst(substr($getParams['entityType'], 8)); } unset($params['entity_id'], $params['entity_table']); foreach ($params as $id => $param) { if ($param && substr($id, 0, 6) == 'return') { $id = substr($id, 7); list($c, $i) = CRM_Utils_System::explode('_', $id, 2); if ($c == 'custom' && is_numeric($i)) { $names['custom_' . $i] = 'custom_' . $i; $id = $i; } else { // Lookup names if ID was not supplied list($group, $field) = CRM_Utils_System::explode(':', $id, 2); $id = CRM_Core_BAO_CustomField::getCustomFieldID($field, $group); if (!$id) { continue; } $names['custom_' . $id] = 'custom_' . $i; } $getParams['custom_' . $id] = 1; } } $result = CRM_Core_BAO_CustomValueTable::getValues($getParams); if ($result['is_error']) { if ($result['error_message'] == "No values found for the specified entity ID and custom field(s).") { $values = array(); return civicrm_api3_create_success($values, $params, 'CustomValue'); } else { throw new API_Exception($result['error_message']); } } else { $entity_id = $result['entityID']; unset($result['is_error'], $result['entityID']); // Convert multi-value strings to arrays $sp = CRM_Core_DAO::VALUE_SEPARATOR; foreach ($result as $id => $value) { if (strpos($value, $sp) !== FALSE) { $value = explode($sp, trim($value, $sp)); } $idArray = explode('_', $id); if ($idArray[0] != 'custom') { continue; } $fieldNumber = $idArray[1]; $customFieldInfo = CRM_Core_BAO_CustomField::getNameFromID($fieldNumber); $info = array_pop($customFieldInfo); // id is the index for returned results if (empty($idArray[2])) { $n = 0; $id = $fieldNumber; } else { $n = $idArray[2]; $id = $fieldNumber . "." . $idArray[2]; } if (!empty($params['format.field_names'])) { $id = $info['field_name']; } else { $id = $fieldNumber; } $values[$id]['entity_id'] = $getParams['entityID']; if (!empty($getParams['entityType'])) { $values[$id]['entity_table'] = $getParams['entityType']; } //set 'latest' -useful for multi fields but set for single for consistency $values[$id]['latest'] = $value; $values[$id]['id'] = $id; $values[$id][$n] = $value; } return civicrm_api3_create_success($values, $params, 'CustomValue'); } }