From 7c2971c26b64328fd00230180e4d5a444684238a Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Sun, 26 Mar 2023 16:46:08 -0400 Subject: [PATCH] SearchKit - Fix handing of new EntityReference custom fields Fixes the metadata for the new EntityReference custom fields to correctly display. Removes redundant function SpecFormatter::customFieldHasOptions which was completely identical to CRM_Core_BAO_CustomField::hasOptions except for one line which was incorrect. --- Civi/Api4/Service/Spec/SpecFormatter.php | 24 +-------- ext/search_kit/Civi/Search/Admin.php | 5 +- .../v4/Custom/CustomEntityReferenceTest.php | 52 +++++++++++++++++++ 3 files changed, 56 insertions(+), 25 deletions(-) create mode 100644 tests/phpunit/api/v4/Custom/CustomEntityReferenceTest.php diff --git a/Civi/Api4/Service/Spec/SpecFormatter.php b/Civi/Api4/Service/Spec/SpecFormatter.php index 3b9ebb4e22..4f32c10772 100644 --- a/Civi/Api4/Service/Spec/SpecFormatter.php +++ b/Civi/Api4/Service/Spec/SpecFormatter.php @@ -48,7 +48,7 @@ class SpecFormatter { $field->setLabel($data['custom_group_id.title'] . ': ' . $data['label']); $field->setHelpPre($data['help_pre'] ?? NULL); $field->setHelpPost($data['help_post'] ?? NULL); - if (self::customFieldHasOptions($data)) { + if (\CRM_Core_BAO_CustomField::hasOptions($data)) { $field->setOptionsCallback([__CLASS__, 'getOptions']); $suffixes = ['label']; if (!empty($data['option_group_id'])) { @@ -103,28 +103,6 @@ class SpecFormatter { return $field; } - /** - * Does this custom field have options - * - * @param array $field - * @return bool - */ - private static function customFieldHasOptions($field) { - // This will include boolean fields with Yes/No options. - if (in_array($field['html_type'], ['Radio', 'CheckBox'])) { - return TRUE; - } - // Do this before the "Select" string search because date fields have a "Select Date" html_type - // and contactRef fields have an "Autocomplete-Select" html_type - contacts are an FK not an option list. - if (in_array($field['data_type'], ['ContactReference', 'Date'])) { - return FALSE; - } - if (strpos($field['html_type'], 'Select') !== FALSE) { - return TRUE; - } - return !empty($field['option_group_id']); - } - /** * Get the data type from an array. Defaults to 'data_type' with fallback to * mapping for the integer value 'type' diff --git a/ext/search_kit/Civi/Search/Admin.php b/ext/search_kit/Civi/Search/Admin.php index ee85568afd..4967484f6f 100644 --- a/ext/search_kit/Civi/Search/Admin.php +++ b/ext/search_kit/Civi/Search/Admin.php @@ -191,9 +191,10 @@ class Admin { foreach (array_reverse($entity['fields'], TRUE) as $index => $field) { if (!empty($field['fk_entity']) && !$field['options'] && !empty($schema[$field['fk_entity']]['label_field'])) { $isCustom = strpos($field['name'], '.'); - // Custom fields: append "Contact ID" to original field label + // Custom fields: append "Contact ID" etc. to original field label if ($isCustom) { - $entity['fields'][$index]['label'] .= ' ' . E::ts('Contact ID'); + $idField = array_column($schema[$field['fk_entity']]['fields'], NULL, 'name')['id']; + $entity['fields'][$index]['label'] .= ' ' . $idField['title']; } // DAO fields: use title instead of label since it represents the id (title usually ends in ID but label does not) else { diff --git a/tests/phpunit/api/v4/Custom/CustomEntityReferenceTest.php b/tests/phpunit/api/v4/Custom/CustomEntityReferenceTest.php new file mode 100644 index 0000000000..dda892cccf --- /dev/null +++ b/tests/phpunit/api/v4/Custom/CustomEntityReferenceTest.php @@ -0,0 +1,52 @@ +setValues([ + 'title' => 'EntityRefFields', + 'extends' => 'Individual', + ])->execute(); + CustomField::create()->setValues([ + 'label' => 'TestActivityReference', + 'custom_group_id.name' => 'EntityRefFields', + 'html_type' => 'Autocomplete-Select', + 'data_type' => 'EntityReference', + 'fk_entity' => 'Activity', + ])->execute(); + $spec = Contact::getFields(FALSE) + ->addWhere('name', '=', 'EntityRefFields.TestActivityReference') + ->execute()->single(); + $this->assertNull($spec['suffixes']); + $this->assertEquals('EntityRef', $spec['input_type']); + $this->assertEquals('Activity', $spec['fk_entity']); + } + +} -- 2.25.1