Merge pull request #11407 from cividesk/CRM-21553
[civicrm-core.git] / api / v3 / CustomValue.php
index 814e7faa6aad37faae8eafa8f155615d4bc91fb4..20aa18c51880299636d94f8bcec81b8bf973285e 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.7                                                |
+ | CiviCRM version 5                                                  |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2017                                |
  +--------------------------------------------------------------------+
@@ -145,22 +145,27 @@ function civicrm_api3_custom_value_get($params) {
   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;
+      $returnVal = $param;
+      if (!empty(substr($id, 7))) {
+        $returnVal = substr($id, 7);
       }
-      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;
+      foreach ((array) $returnVal as $value) {
+        list($c, $i) = CRM_Utils_System::explode('_', $value, 2);
+        if ($c == 'custom' && is_numeric($i)) {
+          $names['custom_' . $i] = 'custom_' . $i;
+          $fldId = $i;
+        }
+        else {
+          // Lookup names if ID was not supplied
+          list($group, $field) = CRM_Utils_System::explode(':', $value, 2);
+          $fldId = CRM_Core_BAO_CustomField::getCustomFieldID($field, $group);
+          if (!$fldId) {
+            continue;
+          }
+          $names['custom_' . $fldId] = 'custom_' . $i;
         }
-        $names['custom_' . $id] = 'custom_' . $i;
+        $getParams['custom_' . $fldId] = 1;
       }
-      $getParams['custom_' . $id] = 1;
     }
   }
 
@@ -363,14 +368,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;