Search Ext: fix loading options and parsing custom field names
authorColeman Watts <coleman@civicrm.org>
Thu, 16 Jul 2020 18:18:48 +0000 (14:18 -0400)
committerColeman Watts <coleman@civicrm.org>
Thu, 16 Jul 2020 18:18:48 +0000 (14:18 -0400)
ext/search/ang/search.module.js
ext/search/ang/search/crmSearch.component.js

index 433383ed68aaa3a8d81363ff62cf3a0e90d8220d..f80d290b98c56ed5e3b1d9785254a54293b3c584 100644 (file)
           joinEntity = dotSplit.length > 1 ? dotSplit[0] : null,
           fieldName = _.last(dotSplit).split(':')[0],
           entityName = searchEntity;
+        // Custom fields contain a dot in their fieldname
+        // If 3 segments, the first is the joinEntity and the last 2 are the custom field
+        if (dotSplit.length === 3) {
+          fieldName = dotSplit[1] + '.' + fieldName;
+        }
+        // If 2 segments, it's ambiguous whether this is a custom field or joined field. Search the main entity first.
+        if (dotSplit.length === 2) {
+          var field = _.find(getEntity(true).fields, {name: dotSplit[0] + '.' + fieldName});
+          if (field) {
+            return field;
+          }
+        }
         if (joinEntity) {
           entityName = _.find(CRM.vars.search.links[entityName], {alias: joinEntity}).entity;
         }
@@ -57,9 +69,9 @@
           }
           result.field = getField(fieldName);
           var split = fieldName.split(':'),
-            dotPos = split[0].indexOf('.');
+            prefixPos = split[0].indexOf(result.field.name);
           result.path = split[0];
-          result.prefix = result.path.substring(0, dotPos + 1);
+          result.prefix = prefixPos > 0 ? result.path.substring(0, prefixPos) : '';
           result.suffix = !split[1] ? '' : ':' + split[1];
           return result;
         }
index 300093736b3afc08fe2208e420dce44f4fbd6a75..3ec65608b00025ea4568822c2d5480097281542c 100644 (file)
       }
 
       this.$onInit = function() {
-        loadFieldOptions();
-
         $scope.$bindToRoute({
           expr: '$ctrl.params.select',
           param: 'select',
           });
         }
         $scope.$watch('$ctrl.params.having', onChangeFilters, true);
+
+        loadFieldOptions();
       };
 
       $scope.saveGroup = function() {