From ceb8c84366a62ede3dfeb7e433b13097d9719524 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Wed, 22 Feb 2023 16:01:07 -0500 Subject: [PATCH] Afform - Filter autocomplete fields belonging to a search display Fixes dev/core#4138 --- .../Api4/Subscriber/AfformAutocompleteSubscriber.php | 11 +++++++++-- ext/afform/core/ang/af/fields/EntityRef.html | 2 +- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/ext/afform/core/Civi/Api4/Subscriber/AfformAutocompleteSubscriber.php b/ext/afform/core/Civi/Api4/Subscriber/AfformAutocompleteSubscriber.php index c94d392e50..70a1bc05ef 100644 --- a/ext/afform/core/Civi/Api4/Subscriber/AfformAutocompleteSubscriber.php +++ b/ext/afform/core/Civi/Api4/Subscriber/AfformAutocompleteSubscriber.php @@ -78,8 +78,15 @@ class AfformAutocompleteSubscriber extends AutoService implements EventSubscribe [$entityName, $joinEntity] = array_pad(explode('+', $entityName), 2, NULL); $entity = $formDataModel->getEntity($entityName); + // If no model entity, it's a search display + if (!$entity) { + $searchDisplay = $formDataModel->getSearchDisplay($entityName); + $apiEntity = civicrm_api4('SavedSearch', 'get', ['where' => [['name', '=', $searchDisplay['searchName']]]]) + ->first()['api_entity'] ?? NULL; + $formField = $searchDisplay['fields'][$fieldName]['defn'] ?? []; + } // If using a join (e.g. Contact -> Email) - if ($joinEntity) { + elseif ($joinEntity) { $apiEntity = $joinEntity; $isId = FALSE; $formField = $entity['joins'][$joinEntity]['fields'][$fieldName]['defn'] ?? []; @@ -97,7 +104,7 @@ class AfformAutocompleteSubscriber extends AutoService implements EventSubscribe // For the "Existing Entity" selector, // Look up the "type" fields (e.g. contact_type, activity_type_id, case_type_id, etc) // And apply it as a filter if specified on the form. - if ($isId) { + if ($isId && $entity) { if ($entity['type'] === 'Contact') { $typeFields = ['contact_type', 'contact_sub_type']; } diff --git a/ext/afform/core/ang/af/fields/EntityRef.html b/ext/afform/core/ang/af/fields/EntityRef.html index 1b783ff44c..38447c8eaf 100644 --- a/ext/afform/core/ang/af/fields/EntityRef.html +++ b/ext/afform/core/ang/af/fields/EntityRef.html @@ -5,7 +5,7 @@ ng-model="getSetSelect" ng-model-options="{getterSetter: true}" crm-autocomplete="$ctrl.defn.fk_entity" - crm-autocomplete-params="{formName: 'afform:' + $ctrl.afFieldset.getFormName(), fieldName: $ctrl.afFieldset.modelName + ':' + $ctrl.fieldName}" + crm-autocomplete-params="{formName: 'afform:' + $ctrl.afFieldset.getFormName(), fieldName: $ctrl.afFieldset.getName() + ':' + $ctrl.fieldName}" multi="$ctrl.defn.input_attrs.multiple" auto-open="$ctrl.defn.input_attrs.autoOpen" placeholder="{{:: $ctrl.defn.input_attrs.placeholder }}" -- 2.25.1