From c5d8a153acd2e38f600fda99d484c5da61f286cc Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Fri, 20 Jan 2023 18:57:33 -0500 Subject: [PATCH] APIv4 - Support autocompletes of the Entity entity Allows browsing API entities with the APIv4 autocomplete select --- Civi/Api4/Entity.php | 11 ++++ .../Generic/Traits/ArrayQueryActionTrait.php | 3 +- .../EntityAutocompleteProvider.php | 53 +++++++++++++++++++ 3 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 Civi/Api4/Service/Autocomplete/EntityAutocompleteProvider.php diff --git a/Civi/Api4/Entity.php b/Civi/Api4/Entity.php index 6b84d73b9ce7..5f36793aa7fc 100644 --- a/Civi/Api4/Entity.php +++ b/Civi/Api4/Entity.php @@ -15,6 +15,8 @@ * * @see \Civi\Api4\Generic\AbstractEntity * + * @primaryKey name + * @labelField title_plural * @searchable none * @since 5.19 * @package Civi\Api4 @@ -147,6 +149,15 @@ public static function getFields($checkPermissions = TRUE) { }))->setCheckPermissions($checkPermissions); } + /** + * @param bool $checkPermissions + * @return Generic\AutocompleteAction + */ + public static function autocomplete($checkPermissions = TRUE) { + return (new Generic\AutocompleteAction('Entity', __FUNCTION__)) + ->setCheckPermissions($checkPermissions); + } + /** * @param bool $checkPermissions * @deprecated diff --git a/Civi/Api4/Generic/Traits/ArrayQueryActionTrait.php b/Civi/Api4/Generic/Traits/ArrayQueryActionTrait.php index b740f5e72ba7..dc33d11885c0 100644 --- a/Civi/Api4/Generic/Traits/ArrayQueryActionTrait.php +++ b/Civi/Api4/Generic/Traits/ArrayQueryActionTrait.php @@ -178,7 +178,8 @@ public static function filterCompare($row, $condition) { return in_array($expected, $value); } elseif (is_string($value) || is_numeric($value)) { - return strpos((string) $value, (string) $expected) !== FALSE; + // Lowercase check if string contains string + return strpos(strtolower((string) $value), strtolower((string) $expected)) !== FALSE; } return $value == $expected; diff --git a/Civi/Api4/Service/Autocomplete/EntityAutocompleteProvider.php b/Civi/Api4/Service/Autocomplete/EntityAutocompleteProvider.php new file mode 100644 index 000000000000..652cb4c2a0be --- /dev/null +++ b/Civi/Api4/Service/Autocomplete/EntityAutocompleteProvider.php @@ -0,0 +1,53 @@ +display['settings'] || $e->display['type'] !== 'autocomplete' || $e->savedSearch['api_entity'] !== 'Entity') { + return; + } + $e->display['settings'] = [ + 'sort' => [ + ['title_plural', 'ASC'], + ], + 'columns' => [ + [ + 'type' => 'field', + 'key' => 'title_plural', + 'icons' => [ + ['field' => 'icon'], + ], + ], + [ + 'type' => 'field', + 'key' => 'description', + ], + ], + ]; + } + +}