AfformAdmin - Ensure correct filtering for Autocompletes
authorColeman Watts <coleman@civicrm.org>
Thu, 17 Nov 2022 02:59:12 +0000 (21:59 -0500)
committerColeman Watts <coleman@civicrm.org>
Thu, 17 Nov 2022 03:21:50 +0000 (22:21 -0500)
Ensures that both the ajax results and the static options are filtered
by comparing field filters to entity data.

ext/afform/admin/Civi/AfformAdmin/AfformAdminMeta.php
ext/afform/admin/ang/afGuiEditor/afGuiFieldValue.directive.js

index 0f988d9c4ba62e5d8bcf38ada393fbad1379a24c..30b367c90dfa5a2c05eaf6e6003747be898f97a4 100644 (file)
@@ -88,7 +88,7 @@ class AfformAdminMeta {
       'checkPermissions' => FALSE,
       'loadOptions' => ['id', 'label'],
       'action' => 'create',
-      'select' => ['name', 'label', 'input_type', 'input_attrs', 'required', 'options', 'help_pre', 'help_post', 'serialize', 'data_type', 'fk_entity', 'readonly'],
+      'select' => ['name', 'label', 'input_type', 'input_attrs', 'required', 'options', 'help_pre', 'help_post', 'serialize', 'data_type', 'entity', 'fk_entity', 'readonly'],
       'where' => [['input_type', 'IS NOT NULL']],
     ];
     if (in_array($entityName, \CRM_Contact_BAO_ContactType::basicTypes(TRUE), TRUE)) {
index fae80a96897e4ab806cd75cb72784659f80f6d4f..82448377a2567f2c91e9ac2901e02af52493187c 100644 (file)
@@ -19,6 +19,7 @@
 
         function makeWidget(field) {
           var options,
+            filters,
             $el = $($element),
             inputType = field.input_type,
             dataType = field.data_type;
           else if (field.fk_entity || field.options || dataType === 'Boolean') {
             if (field.fk_entity) {
               // Static options for choosing current user or other entities on the form
-              options = field.fk_entity === 'Contact' ? ['user_contact_id'] : [];
+              options = [];
+              filters = (field.input_attrs && field.input_attrs.filter) || {};
+              if (field.fk_entity === 'Contact' && (!filters.contact_type || filters.contact_type === 'Individual')) {
+                options.push('user_contact_id');
+              }
               _.each(ctrl.editor.getEntities({type: field.fk_entity}), function(entity) {
-                 options.push({id: entity.name, label: entity.label, icon: afGui.meta.entities[entity.type].icon});
+                // Check if field filters match entity data (e.g. contact_type)
+                var filtersMatch = true;
+                _.each(filters, function(value, key) {
+                  if (entity.data && entity.data[key] && entity.data[key] != value) {
+                    filtersMatch = false;
+                  }
+                })
+                if (filtersMatch) {
+                  options.push({id: entity.name, label: entity.label, icon: afGui.meta.entities[entity.type].icon});
+                }
               });
               $el.crmAutocomplete(field.fk_entity, {fieldName: field.entity + '.' + field.name}, {
                 multiple: multi,