Autocomplete - Fix incorrect matching on id instead of value
authorcolemanw <coleman@civicrm.org>
Thu, 23 Nov 2023 20:07:58 +0000 (15:07 -0500)
committercolemanw <coleman@civicrm.org>
Fri, 24 Nov 2023 01:31:22 +0000 (20:31 -0500)
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
Civi/Api4/OptionValue.php
Civi/Api4/Service/Autocomplete/OptionValueAutocompleteProvider.php
tests/phpunit/api/v4/Custom/ExportCustomGroupTest.php

index 3943c2a29905b44902277cc1d66223c597185e7f..05d955740f141f429da7f7fe859e1a4c39957a12 100644 (file)
@@ -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);
         }
index 9e2a580c1fc8dc4a6a1594e8f08917b45f9dbfbb..210aa2219ee876d6c62828ddb17b23222f093a64 100644 (file)
@@ -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
  */
index 08e327c3bff937a353667ae742601e7d1c9ec48f..1e9ca36f38c46bbe316b10da22749ad5b19d37db 100644 (file)
@@ -47,6 +47,8 @@ class OptionValueAutocompleteProvider extends \Civi\Core\Service\AutoService imp
         [
           'type' => 'field',
           'key' => 'description',
+          'rewrite' => '#[value] [description]',
+          'empty_value' => '#[value]',
         ],
       ],
     ];
index 2462a2876d543b07bb84a2a4ebf6c5edc85aa53f..b5ef79f5f56998318e9a96ba6e9ec1e15cf1294c 100644 (file)
@@ -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']);