From 7021898771d9c0dac904b04cb66337a9a787e94f Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Sat, 27 Feb 2021 23:33:49 -0500 Subject: [PATCH] Afform - add entityRef widget support. Adds a v3 Afform api solely for the purpose of enabling entityRef widgets, which use APIv3. --- api/v3/Generic/Getlist.php | 4 +- ext/afform/core/api/v3/Afform.php | 119 ++++++++++++++++++++++++++++++ js/Common.js | 1 + 3 files changed, 122 insertions(+), 2 deletions(-) create mode 100644 ext/afform/core/api/v3/Afform.php diff --git a/api/v3/Generic/Getlist.php b/api/v3/Generic/Getlist.php index 917922806d..90278877f8 100644 --- a/api/v3/Generic/Getlist.php +++ b/api/v3/Generic/Getlist.php @@ -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; @@ -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']] ?? ''; } diff --git a/ext/afform/core/api/v3/Afform.php b/ext/afform/core/api/v3/Afform.php new file mode 100644 index 0000000000..66a235555a --- /dev/null +++ b/ext/afform/core/api/v3/Afform.php @@ -0,0 +1,119 @@ +findFilePaths()); + $result = []; + + foreach ($names as $name) { + $info = [ + 'name' => $name, + 'module_name' => _afform_angular_module_name($name, 'camel'), + 'directive_name' => _afform_angular_module_name($name, 'dash'), + ]; + $record = $scanner->getMeta($name); + $result[$name] = array_merge($record, $info); + } + + $allFields = []; + _civicrm_api3_afform_get_spec($allFields); + return _civicrm_api3_basic_array_get('Afform', $params, $result, 'name', array_keys($allFields)); +} + +/** + * @param array $fields + */ +function _civicrm_api3_afform_get_spec(&$fields) { + $fields['name'] = [ + 'title' => 'Name', + 'type' => CRM_Utils_Type::T_STRING, + ]; + $fields['title'] = [ + 'title' => 'Title', + 'type' => CRM_Utils_Type::T_STRING, + ]; + $fields['module_name'] = [ + 'title' => 'Module Name', + 'type' => CRM_Utils_Type::T_STRING, + ]; + $fields['directive_name'] = [ + 'title' => 'Directive Name', + 'type' => CRM_Utils_Type::T_STRING, + ]; + $fields['description'] = [ + 'title' => 'Description', + 'type' => CRM_Utils_Type::T_STRING, + ]; + $fields['server_route'] = [ + 'title' => 'Server Route', + 'type' => CRM_Utils_Type::T_STRING, + ]; + $fields['type'] = [ + 'title' => 'Type', + 'type' => CRM_Utils_Type::T_STRING, + ]; + $fields['is_dashlet'] = [ + 'title' => 'Dashlet', + 'type' => CRM_Utils_Type::T_BOOLEAN, + ]; + $fields['is_public'] = [ + 'title' => 'Public', + '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'])); +} + +/** + * Format output for Afform entityRef 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; +} diff --git a/js/Common.js b/js/Common.js index 1db1f25885..cb3c8d2370 100644 --- a/js/Common.js +++ b/js/Common.js @@ -756,6 +756,7 @@ if (!CRM.vars) CRM.vars = {}; } markup += '
' + (row.color ? ' ' : '') + + (row.icon ? ' ' : '') + _.escape((row.prefix !== undefined ? row.prefix + ' ' : '') + row.label + (row.suffix !== undefined ? ' ' + row.suffix : '')) + '
' + '
'; -- 2.25.1