SearchKit - Fix aggregation issues
authorColeman Watts <coleman@civicrm.org>
Thu, 21 Apr 2022 14:07:44 +0000 (10:07 -0400)
committerColeman Watts <coleman@civicrm.org>
Thu, 21 Apr 2022 14:35:37 +0000 (10:35 -0400)
In the UI, "Extra" fields like Contact.age_years were not being aggregated properly.

On the server, fixes mismatch when checking an aggregate field with a suffix.
Fixes recent test regression.

ext/search_kit/Civi/Api4/Action/SearchDisplay/SavedSearchInspectorTrait.php
ext/search_kit/ang/crmSearchAdmin/crmSearchAdmin.component.js

index a1157b3c072edfcfb4a2f207130f51e94d257fe9..1f935a4619290728e9ea699a8e6731c31616cf50 100644 (file)
@@ -157,8 +157,10 @@ trait SavedSearchInspectorTrait {
    * @return bool
    */
   private function canAggregate($fieldPath) {
-    $apiParams = $this->savedSearch['api_params'] ?? [];
+    // Disregard suffix
+    [$fieldPath] = explode(':', $fieldPath);
     $field = $this->getField($fieldPath);
+    $apiParams = $this->savedSearch['api_params'] ?? [];
 
     // If the query does not use grouping or the field doesn't exist, never
     if (empty($apiParams['groupBy']) || !$field) {
@@ -170,8 +172,7 @@ trait SavedSearchInspectorTrait {
     }
 
     // If the entity this column belongs to is being grouped by id, then also no
-    $suffix = strstr($fieldPath, ':') ?: '';
-    $idField = substr($fieldPath, 0, 0 - strlen($field['name'] . $suffix)) . CoreUtil::getIdFieldName($field['entity']);
+    $idField = substr($fieldPath, 0, 0 - strlen($field['name'])) . CoreUtil::getIdFieldName($field['entity']);
     return !in_array($idField, $apiParams['groupBy']);
   }
 
index bf6311ebbbc1c3c696e8479777efb4ada33343ef..d95e711e8047ba20890fa0f775e1255b27f83fd6 100644 (file)
         }
         var arg = _.findWhere(searchMeta.parseExpr(col).args, {type: 'field'}) || {};
         // If the column is not a database field, no
-        if (!arg.field || !arg.field.entity || !_.includes(['Field', 'Custom'], arg.field.type)) {
+        if (!arg.field || !arg.field.entity || !_.includes(['Field', 'Custom', 'Extra'], arg.field.type)) {
           return false;
         }
         // If the column is used for a groupBy, no