'search' => "Searchable options are returned. Labels are translated.",
'validate' => "All options are returned, even if they are disabled. Machine names are used in place of labels.",
'abbreviate' => "Active options are returned, and labels are replaced with abbreviations.",
+ 'match' => "Enabled options are returned using machine names as keys. Labels are translated.",
);
// Validation: enforce uniformity of this param
if ($context !== NULL && !isset($contexts[$context])) {
if ($context == 'validate') {
$params['labelColumn'] = 'name';
}
+ if ($context == 'match') {
+ $params['keyColumn'] = 'name';
+ }
// Call our generic fn for retrieving from the option_value table
return CRM_Core_OptionGroup::values(
$pseudoconstant['optionGroupName'],
$wheres = array();
$order = "ORDER BY %2";
- // Use machine name instead of label in validate context
- if ($context == 'validate') {
+ // Use machine name in certain contexts
+ if ($context == 'validate' || $context == 'match') {
+ $nameField = $context == 'validate' ? 'labelColumn' : 'keyColumn';
if (!empty($pseudoconstant['nameColumn'])) {
- $params['labelColumn'] = $pseudoconstant['nameColumn'];
+ $params[$nameField] = $pseudoconstant['nameColumn'];
}
elseif (in_array('name', $availableFields)) {
- $params['labelColumn'] = 'name';
+ $params[$nameField] = 'name';
}
}
// Condition param can be passed as an sql clause string or an array of clauses
if (!isset($fieldSpec['name'])) {
$metadata[$fieldname]['name'] = $fieldname;
}
- _civicrm_api3_generic_get_metadata_options($metadata, $apiRequest, $fieldname, $fieldSpec, $optionsToResolve);
+ _civicrm_api3_generic_get_metadata_options($metadata, $apiRequest, $fieldname, $fieldSpec);
// Convert options to "sequential" format
if ($sequential && !empty($metadata[$fieldname]['options'])) {
* Field currently being processed.
* @param array $fieldSpec
* Metadata for that field.
- * @param array $fieldsToResolve
- * Any field resolutions specifically requested.
*/
-function _civicrm_api3_generic_get_metadata_options(&$metadata, $apiRequest, $fieldname, $fieldSpec, $fieldsToResolve) {
+function _civicrm_api3_generic_get_metadata_options(&$metadata, $apiRequest, $fieldname, $fieldSpec) {
if (empty($fieldSpec['pseudoconstant']) && empty($fieldSpec['option_group_id'])) {
return;
}
+ $fieldsToResolve = $apiRequest['params']['options']['get_options'];
+
if (!empty($metadata[$fieldname]['options']) || (!in_array($fieldname, $fieldsToResolve) && !in_array('all', $fieldsToResolve))) {
return;
}
- $options = civicrm_api($apiRequest['entity'], 'getoptions', array('version' => 3, 'field' => $fieldname));
+ // Allow caller to specify context
+ $context = CRM_Utils_Array::value('get_options_context', $apiRequest['params']['options']);
+ // Default to api action if it is a supported context.
+ if (!$context) {
+ $action = CRM_Utils_Array::value('action', $apiRequest['params']);
+ $contexts = CRM_Core_DAO::buildOptionsContext();
+ if (isset($contexts[$action])) {
+ $context = $action;
+ }
+ }
+
+ $options = civicrm_api($apiRequest['entity'], 'getoptions', array('version' => 3, 'field' => $fieldname, 'context' => $context));
if (is_array(CRM_Utils_Array::value('values', $options))) {
$metadata[$fieldname]['options'] = $options['values'];
}
showFields(['api_action']);
return;
}
- CRM.api3(entity, 'getFields', {'api_action': action, options: {get_options: 'all'}}).done(function(data) {
+ CRM.api3(entity, 'getfields', {'api_action': action, options: {get_options: 'all', get_options_context: 'match'}}).done(function(data) {
_.each(data.values, function(field) {
if (field.name) {
getFieldData[field.name] = field;