SearchKit - Ensure result autocompleted by id is excluded from subsequent pages
authorcolemanw <coleman@civicrm.org>
Sun, 3 Dec 2023 23:36:10 +0000 (23:36 +0000)
committercolemanw <coleman@civicrm.org>
Sun, 3 Dec 2023 23:36:10 +0000 (23:36 +0000)
Civi/Api4/Generic/AutocompleteAction.php
tests/phpunit/api/v4/Action/AutocompleteTest.php

index 183043e3a851a063f218e0f6bc2f6d627c8896fd..332ea6b42616bf335d23e19995b352c40e8096a0 100644 (file)
@@ -169,6 +169,8 @@ class AutocompleteAction extends AbstractAction {
       // For subsequent pages when searching by id, subtract the "extra" first page
       elseif ($searchById && $this->page > 1) {
         $this->page -= 1;
+        // Record with that id was already returned on page one so exclude it from subsequent pages
+        $this->savedSearch['api_params']['where'][] = [$primaryKeys[0], '!=', $this->input];
       }
       // If first line uses a rewrite, search on those fields too
       if (!$initialSearchById && !empty($this->display['settings']['columns'][0]['rewrite'])) {
index da756de26713b22c33cd3d667a35478eba036583..032ca9db3de8e9fc7949b8ecaadd011160f30c03 100644 (file)
@@ -285,6 +285,7 @@ class AutocompleteTest extends Api4TestBase implements HookInterface, Transactio
     $cid = $contacts[11]['id'];
 
     Contact::save(FALSE)
+      ->addRecord(['id' => $contacts[11]['id'], 'last_name' => "Aaaac$cid"])
       ->addRecord(['id' => $contacts[0]['id'], 'last_name' => "Aaaac$cid"])
       ->addRecord(['id' => $contacts[14]['id'], 'last_name' => "Aaaab$cid"])
       ->addRecord(['id' => $contacts[6]['id'], 'last_name' => "Aaaaa$cid"])