Skip to content
Snippets Groups Projects
Unverified Commit af09a3d8 authored by colemanw's avatar colemanw Committed by GitHub
Browse files

Merge pull request #23013 from colemanw/fixSearchAfformFilterBackport

SearchKit - Fix multi-valued afform filters
parents 366a1ef2 c2a0c784
Branches
Tags
No related merge requests found
<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}}" />
......
......@@ -1013,22 +1013,28 @@ abstract class AbstractRunAction extends \Civi\Api4\Generic\AbstractAction {
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];
}
}
}
}
......
......@@ -46,9 +46,7 @@ class SearchAfformTest extends \PHPUnit\Framework\TestCase implements HeadlessIn
'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' => [
[
......@@ -147,6 +145,12 @@ class SearchAfformTest extends \PHPUnit\Framework\TestCase implements HeadlessIn
$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'];
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment