SearchKit - Fix multi-valued afform filters
authorColeman Watts <coleman@civicrm.org>
Wed, 23 Mar 2022 01:26:16 +0000 (21:26 -0400)
committerColeman Watts <coleman@civicrm.org>
Wed, 23 Mar 2022 01:26:16 +0000 (21:26 -0400)
Multi-select search filters stopped working as of 007167dfb90eaaeb0c76f2c9e5b0327f0b3e22e9

ext/afform/mock/ang/testContactEmailSearchForm.aff.html
ext/search_kit/Civi/Api4/Action/SearchDisplay/AbstractRunAction.php
ext/search_kit/tests/phpunit/api/v4/SearchDisplay/SearchAfformTest.php

index 2f3b1f8814debbe9ce139598a80ead8ac2d60564..390be362f67d59238af2142aebc6346e7418333d 100644 (file)
@@ -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}}" />
index 4d95db8520d34047b4c718c6f6f625b1c359d1ba..8a24c6b72f618047d83ccbc482c4a11b38278a32 100644 (file)
@@ -1014,22 +1014,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];
+          }
         }
       }
     }
index 62ced4717894e5928f0d36499ee2e782b3a69d66..577589455467aa213607dccc287d4639eaede599 100644 (file)
@@ -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'];