Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dev/core#5453 - Fix SearchKit links when using group_by #31211

Merged
merged 1 commit into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Civi/Api4/Generic/Traits/SavedSearchInspectorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down
110 changes: 110 additions & 0 deletions ext/search_kit/tests/phpunit/api/v4/SearchDisplay/SearchRunTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down