dev/core#4759 - Respect Autocomplete Contact Search preferences in menubar quicksearch
authorcolemanw <coleman@civicrm.org>
Mon, 13 Nov 2023 19:43:40 +0000 (14:43 -0500)
committercolemanw <coleman@civicrm.org>
Mon, 13 Nov 2023 19:43:40 +0000 (14:43 -0500)
Civi/Api4/Service/Autocomplete/ContactAutocompleteProvider.php
js/crm.menubar.js

index 27440a78241dda585008ddd48226ec5617dada0e..c031c25821c31c836af7db4b327d80ed9481c5ba 100644 (file)
@@ -13,6 +13,7 @@
 namespace Civi\Api4\Service\Autocomplete;
 
 use Civi\API\Event\PrepareEvent;
+use Civi\Api4\Setting;
 use Civi\Api4\Utils\CoreUtil;
 use Civi\Core\Event\GenericHookEvent;
 use Civi\Core\HookInterface;
@@ -78,22 +79,48 @@ class ContactAutocompleteProvider extends \Civi\Core\Service\AutoService impleme
     // as the menubar autocomplete does not support descriptions
     if (($e->context['formName'] ?? NULL) === 'crmMenubar' && ($e->context['fieldName'] ?? NULL) === 'crm-qsearch-input') {
       $column = ['type' => 'field'];
-      // If doing a search by a field other than the default
+      // Map contact_autocomplete_options settings to v4 format
+      $autocompleteOptionsMap = [
+        2 => 'email_primary.email',
+        3 => 'phone_primary.phone',
+        4 => 'address_primary.street_address',
+        5 => 'address_primary.city',
+        6 => 'address_primary.state_province_id:abbr',
+        7 => 'address_primary.country_id:label',
+        8 => 'address_primary.postal_code',
+      ];
+      // If doing a search by a field other than the default,
+      // add that field to the main column
       if (!empty($e->context['filters'])) {
         $filterField = array_keys($e->context['filters'])[0];
       }
       elseif (\Civi::settings()->get('includeEmailInName')) {
         $filterField = 'email_primary.email';
       }
-      if ($filterField) {
+      // Search on name + filter/email
+      if (!empty($filterField)) {
         $column['key'] = $filterField;
         $column['rewrite'] = "[sort_name] :: [$filterField]";
         $column['empty_value'] = '[sort_name]';
+        $autocompleteOptionsMap = array_diff($autocompleteOptionsMap, [$filterField]);
       }
+      // No filter & email search disabled: search on name only
       else {
         $column['key'] = 'sort_name';
       }
       $e->display['settings']['columns'] = [$column];
+      // Add exta columns based on search preferences
+      $autocompleteOptions = Setting::get(FALSE)
+        ->addSelect('contact_autocomplete_options')->execute()
+        ->first();
+      foreach ($autocompleteOptions['value'] ?? [] as $option) {
+        if (isset($autocompleteOptionsMap[$option])) {
+          $e->display['settings']['columns'][] = [
+            'type' => 'field',
+            'key' => $autocompleteOptionsMap[$option],
+          ];
+        }
+      }
     }
   }
 
index d5e98fd532a60f1b8b66cd51ec71a85de5b987fc..9da2de77cc79d697bc3f76f6338811865b12fad2 100644 (file)
             } else {
               params.filters[option.val()] = request.term;
             }
+            // Specialized Autocomplete SearchDisplay: @see ContactAutocompleteProvider
             CRM.api4('Contact', 'autocomplete', params).then(function(result) {
               var ret = [];
               if (result.length > 0) {
                 $('#crm-qsearch-input').autocomplete('widget').menu('option', 'disabled', false);
                 $.each(result, function(key, item) {
-                  ret.push({value: item.id, label: item.label});
+                  // Add extra items from the description (see contact_autocomplete_options setting)
+                  let description = (item.description || []).filter((v) => v);
+                  let extra = description.length ? ' :: ' + description.join(' :: ') : '';
+                  ret.push({value: item.id, label: item.label + extra});
                 });
               } else {
                 $('#crm-qsearch-input').autocomplete('widget').menu('option', 'disabled', true);