Skip to content

Commit

Permalink
Afform - Improve entityRef
Browse files Browse the repository at this point in the history
  • Loading branch information
colemanw committed Feb 28, 2021
1 parent 934cd39 commit 0b0f318
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
4 changes: 2 additions & 2 deletions api/v3/Generic/Getlist.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function civicrm_api3_generic_getList($apiRequest) {
$meta = civicrm_api3_generic_getfields(['action' => 'get'] + $apiRequest, FALSE);

// If the user types an integer into the search
$forceIdSearch = empty($request['id']) && !empty($request['input']) && CRM_Utils_Rule::positiveInteger($request['input']);
$forceIdSearch = empty($request['id']) && !empty($request['input']) && !empty($meta['id']) && CRM_Utils_Rule::positiveInteger($request['input']);
// Add an extra page of results for the record with an exact id match
if ($forceIdSearch) {
$request['page_num'] = ($request['page_num'] ?? 1) - 1;
Expand Down Expand Up @@ -230,7 +230,7 @@ function _civicrm_api3_generic_getlist_output($result, $request, $entity, $field
}
}
}
};
}
if (!empty($request['image_field'])) {
$data['image'] = $row[$request['image_field']] ?? '';
}
Expand Down
50 changes: 47 additions & 3 deletions ext/afform/core/api/v3/Afform.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ function civicrm_api3_afform_get($params) {
$result[$name] = array_merge($record, $info);
}

$filterableFields = ['name', 'module_name', 'directive_name', 'server_route', 'type', 'description', 'title'];
return _civicrm_api3_basic_array_get('Afform', $params, $result, 'name', $filterableFields);
$allFields = [];
_civicrm_api3_afform_get_spec($allFields);
return _civicrm_api3_basic_array_get('Afform', $params, $result, 'name', array_keys($allFields));
}

function _civicrm_api3_afform_get_spec(&$fields) {
$fields['name'] = [
'title' => 'Name',
'api.aliases' => ['id'],
'type' => CRM_Utils_Type::T_STRING,
];
$fields['title'] = [
Expand Down Expand Up @@ -68,3 +68,47 @@ function _civicrm_api3_afform_get_spec(&$fields) {
'type' => CRM_Utils_Type::T_BOOLEAN,
];
}

/**
* Augment parameters for Afform entityRef list.
*
* @see _civicrm_api3_generic_getlist_params
*
* @param array $request
* API request.
*/
function _civicrm_api3_afform_getlist_params(&$request) {
$fieldsToReturn = ['name', 'title', 'type', 'description'];
$request['params']['return'] = array_unique(array_merge($fieldsToReturn, $request['extra']));
$request['params']['options']['sort'] = 'activity_date_time DESC';
}

/**
* Get output for activity list.
*
* @see _civicrm_api3_generic_getlist_output
*
* @param array $result
* @param array $request
*
* @return array
*/
function _civicrm_api3_afform_getlist_output($result, $request) {
$output = [];
if (!empty($result['values'])) {
$icons = CRM_Core_OptionGroup::values('afform_type', FALSE, FALSE, FALSE, NULL, 'icon', FALSE);
foreach ($result['values'] as $row) {
$data = [
'id' => $row[$request['id_field']],
'label' => $row[$request['label_field']],
'description' => [],
'icon' => $icons[$row['type']],
];
if (!empty($row['description'])) {
$data['description'][] = $row['description'];
}
$output[] = $data;
}
}
return $output;
}
1 change: 1 addition & 0 deletions js/Common.js
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,7 @@ if (!CRM.vars) CRM.vars = {};
}
markup += '<div><div class="crm-select2-row-label '+(row.label_class || '')+'">' +
(row.color ? '<span class="crm-select-item-color" style="background-color: ' + row.color + '"></span> ' : '') +
(row.icon ? '<i class="crm-i ' + row.icon + '" aria-hidden="true"></i> ' : '') +
_.escape((row.prefix !== undefined ? row.prefix + ' ' : '') + row.label + (row.suffix !== undefined ? ' ' + row.suffix : '')) +
'</div>' +
'<div class="crm-select2-row-description">';
Expand Down

0 comments on commit 0b0f318

Please sign in to comment.