From: Coleman Watts Date: Sat, 6 Aug 2022 21:50:40 +0000 (-0400) Subject: Afform - Apply "type" values to autocomplete filters X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=de0cbbea42760526ab313a68c9dbe96352b507bf;p=civicrm-core.git 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. --- 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); } }