From 2dced75c53895af0a6ba66bea67d4a278b9f38de Mon Sep 17 00:00:00 2001 From: colemanw Date: Thu, 23 Nov 2023 15:07:58 -0500 Subject: [PATCH] Autocomplete - Fix incorrect matching on id instead of value Fixes https://lab.civicrm.org/dev/core/-/issues/4799 Custom fields of type Autocomplete-Select were incorrectly using the id of the optionValue. This fixes it to use the value, and also show the value in the autocomplete results. --- Civi/Api4/Event/Subscriber/AutocompleteFieldSubscriber.php | 5 +++++ Civi/Api4/OptionValue.php | 2 +- .../Service/Autocomplete/OptionValueAutocompleteProvider.php | 2 ++ tests/phpunit/api/v4/Custom/ExportCustomGroupTest.php | 2 +- 4 files changed, 9 insertions(+), 2 deletions(-) diff --git a/Civi/Api4/Event/Subscriber/AutocompleteFieldSubscriber.php b/Civi/Api4/Event/Subscriber/AutocompleteFieldSubscriber.php index 3943c2a299..05d955740f 100644 --- a/Civi/Api4/Event/Subscriber/AutocompleteFieldSubscriber.php +++ b/Civi/Api4/Event/Subscriber/AutocompleteFieldSubscriber.php @@ -67,6 +67,11 @@ class AutocompleteFieldSubscriber extends AutoService implements EventSubscriber $apiRequest->addFilter($key, $value); } + // Autocomplete for field with option values + if ($apiRequest->getEntityName() === 'OptionValue' && !empty($fieldSpec['custom_field_id'])) { + $apiRequest->setKey('value'); + } + if ($formType === 'qf') { $this->autocompleteProfilePermissions($apiRequest, $formName, $fieldSpec); } diff --git a/Civi/Api4/OptionValue.php b/Civi/Api4/OptionValue.php index 9e2a580c1f..210aa2219e 100644 --- a/Civi/Api4/OptionValue.php +++ b/Civi/Api4/OptionValue.php @@ -17,7 +17,7 @@ namespace Civi\Api4; * @searchable secondary * @orderBy weight * @groupWeightsBy option_group_id - * @matchFields option_group_id,name + * @matchFields option_group_id,name,value * @since 5.19 * @package Civi\Api4 */ diff --git a/Civi/Api4/Service/Autocomplete/OptionValueAutocompleteProvider.php b/Civi/Api4/Service/Autocomplete/OptionValueAutocompleteProvider.php index 08e327c3bf..1e9ca36f38 100644 --- a/Civi/Api4/Service/Autocomplete/OptionValueAutocompleteProvider.php +++ b/Civi/Api4/Service/Autocomplete/OptionValueAutocompleteProvider.php @@ -47,6 +47,8 @@ class OptionValueAutocompleteProvider extends \Civi\Core\Service\AutoService imp [ 'type' => 'field', 'key' => 'description', + 'rewrite' => '#[value] [description]', + 'empty_value' => '#[value]', ], ], ]; diff --git a/tests/phpunit/api/v4/Custom/ExportCustomGroupTest.php b/tests/phpunit/api/v4/Custom/ExportCustomGroupTest.php index 2462a2876d..b5ef79f5f5 100644 --- a/tests/phpunit/api/v4/Custom/ExportCustomGroupTest.php +++ b/tests/phpunit/api/v4/Custom/ExportCustomGroupTest.php @@ -84,7 +84,7 @@ class ExportCustomGroupTest extends CustomTestBase { $this->assertEquals(['name'], $export[1]['params']['match']); // Match optionValue by name and option_group_id sort($export[2]['params']['match']); - $this->assertEquals(['name', 'option_group_id'], $export[2]['params']['match']); + $this->assertEquals(['name', 'option_group_id', 'value'], $export[2]['params']['match']); // Match customField by name and custom_group_id sort($export[5]['params']['match']); $this->assertEquals(['custom_group_id', 'name'], $export[5]['params']['match']); -- 2.25.1