From fc6a6a51b559276d9dd2e8b2084b4f0fa40e49ed Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Thu, 9 Oct 2014 14:16:00 -0400 Subject: [PATCH] API Explorer - use entityRef for FK fields --- api/v3/utils.php | 11 +++++++- templates/CRM/Admin/Page/APIExplorer.js | 35 +++++++++++++++++-------- 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/api/v3/utils.php b/api/v3/utils.php index 0c6075cf4c..37d232a6e8 100644 --- a/api/v3/utils.php +++ b/api/v3/utils.php @@ -1583,7 +1583,7 @@ function _civicrm_api_get_fields($entity, $unique = FALSE, &$params = array()) { $fields = $d->fields(); // replace uniqueNames by the normal names as the key if (empty($unique)) { - foreach ($fields as $name => & $field) { + foreach ($fields as $name => &$field) { //getting rid of unused attributes foreach ($unsetIfEmpty as $attr) { if (empty($field[$attr])) { @@ -1603,6 +1603,15 @@ function _civicrm_api_get_fields($entity, $unique = FALSE, &$params = array()) { unset($fields[$name]); } } + // Translate FKClassName to the corresponding api + foreach ($fields as $name => &$field) { + if (!empty($field['FKClassName'])) { + $FKApi = CRM_Core_DAO_AllCoreTables::getBriefName($field['FKClassName']); + if ($FKApi) { + $field['FKApiName'] = $FKApi; + } + } + } $fields += _civicrm_api_get_custom_fields($entity, $params); return $fields; } diff --git a/templates/CRM/Admin/Page/APIExplorer.js b/templates/CRM/Admin/Page/APIExplorer.js index 2df88070ab..729d7c6d4e 100644 --- a/templates/CRM/Admin/Page/APIExplorer.js +++ b/templates/CRM/Admin/Page/APIExplorer.js @@ -4,6 +4,7 @@ action, actions = ['get'], fields = [], + getFieldData = {}, options = {}, params = {}, smartyStub, @@ -79,7 +80,7 @@ function getFields() { var required = []; fields = []; - options = {}; + options = getFieldData = {}; // Special case for getfields if (action === 'getfields') { fields.push({ @@ -90,7 +91,8 @@ showFields(['api_action']); return; } - CRM.api3(entity, 'getFields', {'api_action': action, sequential: 1, options: {get_options: 'all'}}).done(function(data) { + CRM.api3(entity, 'getFields', {'api_action': action, options: {get_options: 'all'}}).done(function(data) { + getFieldData = data.values; _.each(data.values, function(field) { if (field.name) { fields.push({ @@ -167,7 +169,7 @@ } /** - * Render value input as a textfield, option list, or hidden, + * Render value input as a textfield, option list, entityRef, or hidden, * Depending on selected param name and operator */ function renderValueField() { @@ -186,8 +188,8 @@ return; } $valField.css('visibility', ''); - // Option list input - if (options[name] && $.inArray(operator, TEXT) < 0) { + // Option list or entityRef input + if ((options[name] || (getFieldData[name] && getFieldData[name].FKApiName)) && $.inArray(operator, TEXT) < 0) { // Reset value before switching to a select from something else if ($(this).is('.api-param-name') || !$valField.data('select2')) { $valField.val(''); @@ -196,12 +198,23 @@ else if (operatorType == 'single' && currentVal.indexOf(',') > -1) { $valField.val(currentVal.split(',')[0]); } - $valField.select2({ - multiple: (operatorType === 'multi'), - data: _.transform(options[name], function(result, option) { - result.push({id: option.key, text: option.value}); - }) - }); + // Select options + if (options[name]) { + + $valField.select2({ + multiple: (operatorType === 'multi'), + data: _.map(options[name], function (value, key) { + return {id: key, text: value}; + }) + }); + } + // EntityRef + else { + $valField.crmEntityRef({ + entity: getFieldData[name].FKApiName, + select: {multiple: (operatorType === 'multi')} + }); + } return; } // Plain text input -- 2.25.1