From de0cbbea42760526ab313a68c9dbe96352b507bf Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Sat, 6 Aug 2022 17:50:40 -0400 Subject: [PATCH] Afform - Apply "type" values to autocomplete filters This ensures that if e.g. a Contact or Activity has its type specified on the form it will also be limited to that type in autocomplete results when selecting an existing entity. --- CRM/Core/BAO/CustomGroup.php | 2 +- .../Subscriber/AutocompleteSubscriber.php | 22 ++++++++++++++----- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/CRM/Core/BAO/CustomGroup.php b/CRM/Core/BAO/CustomGroup.php index 3b6e9269cf..f7c1c6d1af 100644 --- a/CRM/Core/BAO/CustomGroup.php +++ b/CRM/Core/BAO/CustomGroup.php @@ -2473,7 +2473,7 @@ SELECT civicrm_custom_group.id as groupID, civicrm_custom_group.title as groupT [ 'id' => 'Pledge', 'label' => ts('Pledges'), - 'grouping' => 'TODO', + 'grouping' => NULL, 'table_name' => 'civicrm_pledge', ], [ diff --git a/ext/afform/core/Civi/Api4/Subscriber/AutocompleteSubscriber.php b/ext/afform/core/Civi/Api4/Subscriber/AutocompleteSubscriber.php index ece48d0932..685a99c95d 100644 --- a/ext/afform/core/Civi/Api4/Subscriber/AutocompleteSubscriber.php +++ b/ext/afform/core/Civi/Api4/Subscriber/AutocompleteSubscriber.php @@ -46,19 +46,31 @@ class AutocompleteSubscriber implements EventSubscriberInterface { $afform = Afform::get() ->addWhere('name', '=', str_replace('afform:', '', $formName)) ->addSelect('layout') - ->setLayoutFormat('shallow') ->execute()->first(); if (!$afform) { return; } $formDataModel = new FormDataModel($afform['layout']); $entity = $formDataModel->getEntity($entityName); - $defn = []; - if (!empty($entity['fields'][$fieldName]['defn'])) { - $defn = \CRM_Utils_JS::decode($entity['fields'][$fieldName]['defn']); + + // Look up the "type" fields (e.g. contact_type, activity_type_id, case_type_id, etc) + $typeFields = []; + if ($entity['type'] === 'Contact') { + $typeFields = ['contact_type', 'contact_sub_type']; + } + else { + $extends = array_column(\CRM_Core_BAO_CustomGroup::getCustomGroupExtendsOptions(), 'grouping', 'id'); + $typeFields = (array) ($extends[$entity['type']] ?? NULL); } + // If entity has a type set in the values, auto-apply that to filters + foreach ($typeFields as $typeField) { + if (!empty($entity['data'][$typeField])) { + $apiRequest->addFilter($typeField, $entity['data'][$typeField]); + } + } + $apiRequest->setCheckPermissions($entity['security'] !== 'FBAC'); - $apiRequest->setSavedSearch($defn['saved_search'] ?? NULL); + $apiRequest->setSavedSearch($entity['fields'][$fieldName]['defn']['saved_search'] ?? NULL); } } -- 2.25.1