From 9f61f456e9802f9734158ff74387bb8cd2886017 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Sat, 20 Feb 2021 23:03:37 -0500 Subject: [PATCH] EntityRef - Search by id when an integer is entered --- CRM/Contact/BAO/Contact.php | 1 - api/v3/Generic/Getlist.php | 24 +++++++++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/CRM/Contact/BAO/Contact.php b/CRM/Contact/BAO/Contact.php index 05e7a4a2c8..bae495846f 100644 --- a/CRM/Contact/BAO/Contact.php +++ b/CRM/Contact/BAO/Contact.php @@ -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'], ]; diff --git a/api/v3/Generic/Getlist.php b/api/v3/Generic/Getlist.php index f63948e27c..58e8f18fbb 100644 --- a/api/v3/Generic/Getlist.php +++ b/api/v3/Generic/Getlist.php @@ -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]); -- 2.25.1