From 11c1be05826e02c4008a962b9cf497cdd6192d6f Mon Sep 17 00:00:00 2001 From: colemanw Date: Wed, 15 Nov 2023 15:44:37 -0500 Subject: [PATCH] APIv4 - Fix autocomplete filters for multi-record custom groups --- CRM/Core/BAO/CustomField.php | 44 +++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 13 deletions(-) diff --git a/CRM/Core/BAO/CustomField.php b/CRM/Core/BAO/CustomField.php index e6f4b68d31..2fd3c0d1f5 100644 --- a/CRM/Core/BAO/CustomField.php +++ b/CRM/Core/BAO/CustomField.php @@ -1116,18 +1116,23 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField { ); } - elseif ($field->data_type == 'EntityReference') { - $fieldAttributes['entity'] = $field->fk_entity; - $fieldAttributes['api']['fieldName'] = $field->getEntity() . '.' . $groupName . '.' . $field->name; - $element = $qf->addAutocomplete($elementName, $label, $fieldAttributes, $useRequired && !$search); - } - // Autocomplete for field with option values else { - $fieldAttributes['entity'] = 'OptionValue'; - $fieldAttributes['placeholder'] = $placeholder; - $fieldAttributes['api']['fieldName'] = $field->getEntity() . '.' . $groupName . '.' . $field->name; - $fieldAttributes['select']['multiple'] = $search ? TRUE : !empty($field->serialize); - $fieldAttributes['select']['minimumInputLength'] = 0; + $entityName = $field->getEntityName(); + // Format fieldName attribute: convention is `EntityName.field_name` + // For normal entities, `field_name` is a combination of `customGroupName.customFieldName` + // But form multi-record custom groups, the EntityName already includes the customGroupName so field_name stands alone + $fieldAttributes['api']['fieldName'] = $entityName . (str_starts_with($entityName, 'Custom_') ? '' : ".$groupName") . ".$field->name"; + // Autocomplete for FK fields + if ($field->data_type == 'EntityReference') { + $fieldAttributes['entity'] = $field->fk_entity; + } + // Autocomplete for field with option values + else { + $fieldAttributes['entity'] = 'OptionValue'; + $fieldAttributes['placeholder'] = $placeholder; + $fieldAttributes['select']['multiple'] = $search ? TRUE : !empty($field->serialize); + $fieldAttributes['select']['minimumInputLength'] = 0; + } $element = $qf->addAutocomplete($elementName, $label, $fieldAttributes, $useRequired && !$search); } @@ -2863,8 +2868,8 @@ WHERE cf.id = %1 AND cg.is_multiple = 1"; } /** - * Get api entity for this field - * + * Get api3 entity name for this field + * @deprecated * @return string */ public function getEntity() { @@ -2872,6 +2877,19 @@ WHERE cf.id = %1 AND cg.is_multiple = 1"; return in_array($entity, CRM_Contact_BAO_ContactType::basicTypes(TRUE), TRUE) ? 'Contact' : $entity; } + /** + * Get api4 entity name for this field + * @return string + */ + public function getEntityName(): string { + $isMultiple = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $this->custom_group_id, 'is_multiple'); + if ($isMultiple) { + $groupName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $this->custom_group_id, 'name'); + return "Custom_$groupName"; + } + return CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $this->custom_group_id, 'extends'); + } + /** * Set pseudoconstant properties for field metadata. * -- 2.25.1