From dbd84de93db50976aa3e3457733c0c161a8097cd Mon Sep 17 00:00:00 2001 From: colemanw Date: Sun, 29 Sep 2024 18:05:40 -0400 Subject: [PATCH] dev/core#5453 - Fix SearchKit links when using group_by --- .../Traits/SavedSearchInspectorTrait.php | 10 ++ .../api/v4/SearchDisplay/SearchRunTest.php | 110 ++++++++++++++++++ 2 files changed, 120 insertions(+) diff --git a/Civi/Api4/Generic/Traits/SavedSearchInspectorTrait.php b/Civi/Api4/Generic/Traits/SavedSearchInspectorTrait.php index dedfa9d54a08..b44649e9b4ed 100644 --- a/Civi/Api4/Generic/Traits/SavedSearchInspectorTrait.php +++ b/Civi/Api4/Generic/Traits/SavedSearchInspectorTrait.php @@ -229,6 +229,16 @@ private function canAggregate($fieldPath) { return FALSE; } + // If this is an implicit join, use the parent field + if (str_ends_with($fieldPath, '.' . $field['name'])) { + $baseFieldPath = substr($fieldPath, 0, -strlen('.' . $field['name'])); + $baseField = $this->getField($baseFieldPath); + if ($baseField) { + $fieldPath = $baseFieldPath; + $field = $baseField; + } + } + // If the entity this column belongs to is being grouped by id, then also no $idField = substr($fieldPath, 0, 0 - strlen($field['name'])) . CoreUtil::getIdFieldName($field['entity']); return !in_array($idField, $apiParams['groupBy']); diff --git a/ext/search_kit/tests/phpunit/api/v4/SearchDisplay/SearchRunTest.php b/ext/search_kit/tests/phpunit/api/v4/SearchDisplay/SearchRunTest.php index 69791a717d22..54ab3502c2f5 100644 --- a/ext/search_kit/tests/phpunit/api/v4/SearchDisplay/SearchRunTest.php +++ b/ext/search_kit/tests/phpunit/api/v4/SearchDisplay/SearchRunTest.php @@ -2294,6 +2294,116 @@ public function testLinkConditions() { $this->assertCount(1, $result[1]['columns'][1]['links']); } + public function testLinksWithGroupBy() { + $contacts = $this->saveTestRecords('Individual', [ + 'records' => [ + ['first_name' => 'A', 'last_name' => 'A'], + ['first_name' => 'B', 'last_name' => 'B'], + ], + ]); + $contributions = $this->saveTestRecords('Contribution', [ + 'records' => [ + ['contact_id' => $contacts[0]['id']], + ['contact_id' => $contacts[1]['id']], + ], + ]); + $params = [ + 'checkPermissions' => FALSE, + 'return' => 'page:1', + 'savedSearch' => [ + 'api_entity' => 'Contribution', + 'api_params' => [ + 'version' => 4, + 'select' => [ + 'id', + 'contact_id', + 'contact_id.sort_name', + 'total_amount', + 'financial_type_id:label', + ], + 'orderBy' => [], + 'where' => [ + ['id', 'IN', $contributions->column('id')], + ], + 'groupBy' => [ + 'id', + ], + 'join' => [ + [ + 'Contact AS Contribution_Contact_contact_id_01', + 'LEFT', + [ + 'contact_id', + '=', + 'Contribution_Contact_contact_id_01.id', + ], + ], + ], + ], + ], + 'display' => [ + 'type' => 'table', + 'label' => 'testDisplay', + 'settings' => [ + 'description' => NULL, + 'sort' => [], + 'limit' => 50, + 'pager' => [], + 'placeholder' => 5, + 'columns' => [ + [ + 'type' => 'field', + 'key' => 'id', + 'dataType' => 'Integer', + 'label' => 'Contribution ID', + 'sortable' => TRUE, + ], + [ + 'type' => 'field', + 'key' => 'contact_id.sort_name', + 'dataType' => 'String', + 'label' => 'Contact Sort Name', + 'sortable' => TRUE, + 'link' => [ + 'path' => '', + 'entity' => 'Contact', + 'action' => 'view', + 'join' => 'contact_id', + 'target' => '', + ], + 'title' => 'View Contact', + ], + [ + 'type' => 'field', + 'key' => 'total_amount', + 'dataType' => 'Money', + 'label' => 'Total Amount', + 'sortable' => TRUE, + ], + [ + 'type' => 'field', + 'key' => 'financial_type_id:label', + 'dataType' => 'Integer', + 'label' => 'Financial Type', + 'sortable' => TRUE, + ], + ], + 'actions' => TRUE, + 'classes' => [ + 'table', + 'table-striped', + ], + ], + ], + ]; + + $result = civicrm_api4('SearchDisplay', 'run', $params); + $this->assertCount(2, $result); + + $this->assertCount(1, $result[0]['columns'][1]['links']); + $this->assertCount(1, $result[1]['columns'][1]['links']); + } + public function testContactTypeIcons(): void { $this->createTestRecord('ContactType', [ 'label' => 'Star',