EntityRef - Search by id when an integer is entered
authorColeman Watts <coleman@civicrm.org>
Sun, 21 Feb 2021 04:03:37 +0000 (23:03 -0500)
committerColeman Watts <coleman@civicrm.org>
Sun, 21 Feb 2021 04:25:23 +0000 (23:25 -0500)
CRM/Contact/BAO/Contact.php
api/v3/Generic/Getlist.php

index 05e7a4a2c85c61c0bb9e99b4acfab77c96f55566..bae495846fff7f23b577e9ac05b5d11fcdf6c768 100644 (file)
@@ -3677,7 +3677,6 @@ LEFT JOIN civicrm_address ON ( civicrm_address.contact_id = civicrm_contact.id )
       ['key' => 'organization_name', 'value' => ts('Employer name'), 'type' => 'text', 'condition' => ['contact_type' => 'Individual']],
       ['key' => 'gender_id', 'value' => ts('Gender'), 'condition' => ['contact_type' => 'Individual']],
       ['key' => 'is_deceased', 'value' => ts('Deceased'), 'condition' => ['contact_type' => 'Individual']],
-      ['key' => 'contact_id', 'value' => ts('Contact ID'), 'type' => 'text'],
       ['key' => 'external_identifier', 'value' => ts('External ID'), 'type' => 'text'],
       ['key' => 'source', 'value' => ts('Contact Source'), 'type' => 'text'],
     ];
index f63948e27c2ce66b5e673aeb281d549dabbb2185..58e8f18fbbdd54e9f2d2b8d4bfcb7375f1c4ff56 100644 (file)
@@ -26,6 +26,17 @@ function civicrm_api3_generic_getList($apiRequest) {
   $request = $apiRequest['params'];
   $meta = civicrm_api3_generic_getfields(['action' => 'get'] + $apiRequest, FALSE);
 
+  // If the user types an integer into the search
+  $forceIdSearch = empty($request['id']) && !empty($request['input']) && CRM_Utils_Rule::positiveInteger($request['input']);
+  // Add an extra page of results for the record with an exact id match
+  if ($forceIdSearch) {
+    $request['page_num'] = ($request['page_num'] ?? 1) - 1;
+    if (empty($request['page_num'])) {
+      $request['id'] = $request['input'];
+      unset($request['input']);
+    }
+  }
+
   // Hey api, would you like to provide default values?
   $fnName = "_civicrm_api3_{$entity}_getlist_defaults";
   $defaults = function_exists($fnName) ? $fnName($request) : [];
@@ -73,8 +84,19 @@ function civicrm_api3_generic_getList($apiRequest) {
 
   $output = ['page_num' => $request['page_num']];
 
+  if ($forceIdSearch) {
+    $output['page_num']++;
+    // When returning the single record matching id
+    if (empty($request['page_num'])) {
+      $output['more_results'] = TRUE;
+      foreach ($values as $i => $value) {
+        $description = ts('ID: %1', [1 => $value['id']]);
+        $values[$i]['description'] = array_merge([$description], $value['description'] ?? []);
+      }
+    }
+  }
   // Limit is set for searching but not fetching by id
-  if (!empty($request['params']['options']['limit'])) {
+  elseif (!empty($request['params']['options']['limit'])) {
     // If we have an extra result then this is not the last page
     $last = $request['params']['options']['limit'] - 1;
     $output['more_results'] = isset($values[$last]);