dev/core#3049 - Update autocomplete custom fields to use APIv4
authorcolemanw <coleman@civicrm.org>
Sat, 15 Jul 2023 17:26:38 +0000 (13:26 -0400)
committercolemanw <coleman@civicrm.org>
Fri, 1 Sep 2023 16:35:16 +0000 (12:35 -0400)
CRM/Core/BAO/CustomField.php
Civi/Api4/Service/Autocomplete/OptionValueAutocompleteProvider.php [new file with mode: 0644]
Civi/Api4/Service/Spec/SpecFormatter.php

index 8c7f3d0aa9ee2a74c5a3f88348140fc6d69ed4af..709161e266af3abacae7c57a13a8c6961969c015 100644 (file)
@@ -1060,17 +1060,14 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
           $fieldAttributes['api']['fieldName'] = $field->getEntity() . '.' . $groupName . '.' . $field->name;
           $element = $qf->addAutocomplete($elementName, $label, $fieldAttributes, $useRequired && !$search);
         }
+        // Autocomplete for field with option values
         else {
-          // FIXME: This won't work with customFieldOptions hook
-          $fieldAttributes += [
-            'entity' => 'OptionValue',
-            'placeholder' => $placeholder,
-            'multiple' => $search ? TRUE : !empty($field->serialize),
-            'api' => [
-              'params' => ['option_group_id' => $field->option_group_id, 'is_active' => 1],
-            ],
-          ];
-          $element = $qf->addEntityRef($elementName, $label, $fieldAttributes, $useRequired && !$search);
+          $fieldAttributes['entity'] = 'OptionValue';
+          $fieldAttributes['placeholder'] = $placeholder;
+          $fieldAttributes['api']['fieldName'] = $field->getEntity() . '.' . $groupName . '.' . $field->name;
+          $fieldAttributes['select']['multiple'] = $search ? TRUE : !empty($field->serialize);
+          $fieldAttributes['select']['minimumInputLength'] = 0;
+          $element = $qf->addAutocomplete($elementName, $label, $fieldAttributes, $useRequired && !$search);
         }
 
         $qf->assign('customUrls', $customUrls);
diff --git a/Civi/Api4/Service/Autocomplete/OptionValueAutocompleteProvider.php b/Civi/Api4/Service/Autocomplete/OptionValueAutocompleteProvider.php
new file mode 100644 (file)
index 0000000..08e327c
--- /dev/null
@@ -0,0 +1,55 @@
+<?php
+
+/*
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC. All rights reserved.                        |
+ |                                                                    |
+ | This work is published under the GNU AGPLv3 license with some      |
+ | permitted exceptions and without any warranty. For full license    |
+ | and copyright information, see https://civicrm.org/licensing       |
+ +--------------------------------------------------------------------+
+ */
+
+namespace Civi\Api4\Service\Autocomplete;
+
+use Civi\Core\Event\GenericHookEvent;
+use Civi\Core\HookInterface;
+
+/**
+ * @service
+ * @internal
+ */
+class OptionValueAutocompleteProvider extends \Civi\Core\Service\AutoService implements HookInterface {
+
+  /**
+   * Provide default SearchDisplay for Country autocompletes
+   *
+   * @param \Civi\Core\Event\GenericHookEvent $e
+   */
+  public static function on_civi_search_defaultDisplay(GenericHookEvent $e) {
+    if ($e->display['settings'] || $e->display['type'] !== 'autocomplete' || $e->savedSearch['api_entity'] !== 'OptionValue') {
+      return;
+    }
+    $e->display['settings'] = [
+      'sort' => [
+        ['weight', 'ASC'],
+        ['label', 'ASC'],
+      ],
+      'extra' => ['color' => 'color'],
+      'columns' => [
+        [
+          'type' => 'field',
+          'key' => 'label',
+          'icons' => [
+            ['field' => 'icon'],
+          ],
+        ],
+        [
+          'type' => 'field',
+          'key' => 'description',
+        ],
+      ],
+    ];
+  }
+
+}
index df137568e3792ded5fc8c8ef0c1ace48fa5efae0..d44fd43f5057e915207d5d4b0cd8a6a3f6075d38 100644 (file)
@@ -282,6 +282,7 @@ class SpecFormatter {
     $map = [
       'Select Date' => 'Date',
       'Link' => 'Url',
+      'Autocomplete-Select' => 'EntityRef',
     ];
     $inputType = $map[$inputType] ?? $inputType;
     if ($dataTypeName === 'ContactReference' || $dataTypeName === 'EntityReference') {
@@ -341,6 +342,11 @@ class SpecFormatter {
         $inputAttrs['filter'] = $filters;
       }
     }
+    // Custom autocompletes
+    if (!empty($data['option_group_id']) && $inputType === 'EntityRef') {
+      $fieldSpec->setFkEntity('OptionValue');
+      $inputAttrs['filter']['option_group_id'] = $data['option_group_id'];
+    }
     $fieldSpec
       ->setInputType($inputType)
       ->setInputAttrs($inputAttrs);