if(isset($dao->count)) {
return $dao->count;
}
- //if custom fields are required we will endeavour to set them . NB passing $entity in might be a bit clunky / unrequired
- if (!empty($entity) && !empty($params['return']) && is_array($params['return'])) {
- foreach ($params['return'] as $return) {
- if (substr($return, 0, 6) == 'custom') {
- $custom = TRUE;
- }
- }
- }
-
$fields = array_keys(_civicrm_api3_build_fields_array($dao, $uniqueFields));
}
}
$result[$dao->id] = $tmp;
- if (!empty($custom)) {
+
+ if(_civicrm_api3_custom_fields_are_required($entity, $params)) {
_civicrm_api3_custom_data_get($result[$dao->id], $entity, $dao->id);
}
}
return $result;
}
+/**
+ * We currently retrieve all custom fields or none at this level so if we know the entity
+ * && it can take custom fields & there is the string 'custom' in their return request we get them all, they are filtered on the way out
+ * @todo filter so only required fields are queried
+ *
+ * @param $params
+ * @param string $entity - entity name in CamelCase
+ *
+ * @return bool
+ */
+function _civicrm_api3_custom_fields_are_required($entity, $params) {
+ if (!array_key_exists($entity, CRM_Core_BAO_CustomQuery::$extendsMap)) {
+ return FALSE;
+ }
+ $options = _civicrm_api3_get_options_from_params($params);
+ //we check for possibility of 'custom' => 1 as well as specific custom fields
+ $returnString = implode('', $options['return']) . implode('', array_keys($options['return']));
+ if(stristr($returnString, 'custom')) {
+ return TRUE;
+ }
+}
/**
* Converts an object to an array
*
}
}
+/**
+ * @param $params
+ * @param $entity
+ */
+function _civicrm_api3_format_params_for_create(&$params, $entity) {
+ $nonGenericEntities = array('Contact', 'Individual', 'Household', 'Organization');
+
+ $customFieldEntities = array_diff_key(CRM_Core_BAO_CustomQuery::$extendsMap, array_fill_keys($nonGenericEntities, 1));
+ if(!array_key_exists($entity, $customFieldEntities)) {
+ return;
+ }
+ $values = array();
+ _civicrm_api3_custom_format_params($params, $values, $entity);
+ $params = array_merge($params, $values);
+}
+
/**
* @deprecated
* This function ensures that we have the right input parameters
* @return array
*/
function _civicrm_api3_basic_create($bao_name, &$params, $entity = NULL) {
-
+ _civicrm_api3_format_params_for_create($params, $entity);
$args = array(&$params);
if (!empty($entity)) {
$ids = array($entity => CRM_Utils_Array::value('id', $params));