From dabf45f088a941cda0f081add70a90e3e5c19d43 Mon Sep 17 00:00:00 2001 From: colemanw Date: Mon, 13 Nov 2023 20:29:23 -0500 Subject: [PATCH] APIv4 - Add autocomplete action to CustomValue --- Civi/Api4/CustomValue.php | 11 +++ .../CustomValueAutocompleteProvider.php | 76 +++++++++++++++++++ .../EntityAutocompleteProvider.php | 2 +- 3 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 Civi/Api4/Service/Autocomplete/CustomValueAutocompleteProvider.php diff --git a/Civi/Api4/CustomValue.php b/Civi/Api4/CustomValue.php index 4201d3397a..1b99edc821 100644 --- a/Civi/Api4/CustomValue.php +++ b/Civi/Api4/CustomValue.php @@ -26,6 +26,17 @@ namespace Civi\Api4; */ class CustomValue { + /** + * @param string $customGroup + * @param bool $checkPermissions + * @return \Civi\Api4\Generic\AutocompleteAction + * @throws \API_Exception + */ + public static function autocomplete(string $customGroup, $checkPermissions = TRUE) { + return (new \Civi\Api4\Generic\AutocompleteAction("Custom_$customGroup", __FUNCTION__)) + ->setCheckPermissions($checkPermissions); + } + /** * @param string $customGroup * @param bool $checkPermissions diff --git a/Civi/Api4/Service/Autocomplete/CustomValueAutocompleteProvider.php b/Civi/Api4/Service/Autocomplete/CustomValueAutocompleteProvider.php new file mode 100644 index 0000000000..a799696edd --- /dev/null +++ b/Civi/Api4/Service/Autocomplete/CustomValueAutocompleteProvider.php @@ -0,0 +1,76 @@ +display['settings'] || $e->display['type'] !== 'autocomplete' || !str_starts_with($e->savedSearch['api_entity'], 'Custom_')) { + return; + } + $customGroupName = explode('_', $e->savedSearch['api_entity'], 2)[1]; + + // Custom groups could contain any fields & we have no idea what's in them + // but this is just the default display and can be overridden. + // Our best guess for a "title" is the first text field in the group + $titleField = \Civi\Api4\CustomValue::getFields('Multi_Stuff', FALSE) + ->addWhere('data_type', '=', 'String') + ->addWhere('input_type', '=', 'Text') + ->execute()->first()['name'] ?? NULL; + + // Include search label of parent entity (e.g. `entity_id.sort_name` for Contact custom groups) + $customGroupExtends = \CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $customGroupName, 'extends', 'name'); + $extendsLabelField = CoreUtil::getInfoItem($customGroupExtends, 'search_fields')[0] ?? NULL; + + if (!$extendsLabelField && !$titleField) { + // Got nothing, fall back to the default which displays id only. + return; + } + + $mainColumn = [ + 'type' => 'field', + 'key' => $titleField ?? "entity_id.$extendsLabelField", + ]; + if ($titleField && $extendsLabelField) { + $mainColumn['rewrite'] = "[entity_id.$extendsLabelField] - [$titleField]"; + } + + $e->display['settings'] = [ + 'sort' => [ + [$mainColumn['key'], 'ASC'], + ], + 'columns' => [ + $mainColumn, + [ + 'type' => 'field', + 'key' => 'id', + 'rewrite' => '#[id]', + ], + ], + ]; + } + +} diff --git a/Civi/Api4/Service/Autocomplete/EntityAutocompleteProvider.php b/Civi/Api4/Service/Autocomplete/EntityAutocompleteProvider.php index 652cb4c2a0..943cdad2b1 100644 --- a/Civi/Api4/Service/Autocomplete/EntityAutocompleteProvider.php +++ b/Civi/Api4/Service/Autocomplete/EntityAutocompleteProvider.php @@ -22,7 +22,7 @@ use Civi\Core\HookInterface; class EntityAutocompleteProvider extends \Civi\Core\Service\AutoService implements HookInterface { /** - * Provide default SearchDisplay for ContactType autocompletes + * Provide default SearchDisplay for autocompletes of API Entity names * * @param \Civi\Core\Event\GenericHookEvent $e */ -- 2.25.1