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

SearchKit - Fix multi-valued afform filters #23012

Merged
merged 1 commit into from
Mar 24, 2022
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
1 change: 1 addition & 0 deletions ext/afform/mock/ang/testContactEmailSearchForm.aff.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<div af-fieldset="">
<af-field name="source" />
<af-field name="contact_type" />
<div class="af-container af-layout-inline">
<af-field name="Contact_Email_contact_id_01.email" />
<af-field name="Contact_Email_contact_id_01.location_type_id" defn="{input_attrs: {multiple: true}}" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1014,22 +1014,28 @@ private function addFilterLabel($fieldName, $value) {
if (!empty($field['options'])) {
$options = civicrm_api4($field['entity'], 'getFields', [
'loadOptions' => TRUE,
'checkPermissions' => FALSE,
'where' => [['name', '=', $field['name']]],
])->first()['options'] ?? [];
if (!empty($options[$value])) {
$this->filterLabels[] = $options[$value];
foreach ((array) $value as $val) {
if (!empty($options[$val])) {
$this->filterLabels[] = $options[$val];
}
}
}
elseif (!empty($field['fk_entity'])) {
$idField = CoreUtil::getIdFieldName($field['fk_entity']);
$labelField = CoreUtil::getInfoItem($field['fk_entity'], 'label_field');
if ($labelField) {
$record = civicrm_api4($field['fk_entity'], 'get', [
'where' => [[$idField, '=', $value]],
$records = civicrm_api4($field['fk_entity'], 'get', [
'checkPermissions' => $this->checkPermissions,
'where' => [[$idField, 'IN', (array) $value]],
'select' => [$labelField],
])->first() ?? NULL;
if (isset($record[$labelField])) {
$this->filterLabels[] = $record[$labelField];
]);
foreach ($records as $record) {
if (isset($record[$labelField])) {
$this->filterLabels[] = $record[$labelField];
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ public function testRunWithAfform() {
'GROUP_CONCAT(DISTINCT Contact_Email_contact_id_01.email) AS GROUP_CONCAT_Contact_Email_contact_id_01_email',
],
'orderBy' => [],
'where' => [
['contact_type:name', '=', 'Individual'],
],
'where' => [],
'groupBy' => ['id'],
'join' => [
[
Expand Down Expand Up @@ -147,6 +145,12 @@ public function testRunWithAfform() {
$result = civicrm_api4('SearchDisplay', 'run', $params);
$this->assertGreaterThan(1, $result->count());

// For a filter with options, ensure labels are set
$params['filters'] = ['contact_type' => ['Individual']];
$result = civicrm_api4('SearchDisplay', 'run', $params);
$this->assertGreaterThan(1, $result->count());
$this->assertEquals(['Individual'], $result->labels);

// Note that filters add a wildcard so the value `afform_test` matches all 3 sample contacts;
// But the Afform markup contains `filters="{last_name: 'AfformTest'}"` which only matches 2.
$params['filters'] = ['source' => 'afform_test'];
Expand Down