diff --git a/CRM/Core/BAO/CustomField.php b/CRM/Core/BAO/CustomField.php index 8d232803944e..1dc707ecd688 100644 --- a/CRM/Core/BAO/CustomField.php +++ b/CRM/Core/BAO/CustomField.php @@ -2156,7 +2156,7 @@ public static function fixOptionGroups($customFieldId, $optionGroupId) { ); // get the updated option group // if both are same return - if ($currentOptionGroupId == $optionGroupId) { + if (!$currentOptionGroupId || $currentOptionGroupId == $optionGroupId) { return; } diff --git a/tests/phpunit/api/v3/CustomFieldTest.php b/tests/phpunit/api/v3/CustomFieldTest.php index 6e822b1bf11a..6b6573e82826 100644 --- a/tests/phpunit/api/v3/CustomFieldTest.php +++ b/tests/phpunit/api/v3/CustomFieldTest.php @@ -300,6 +300,28 @@ public function testCustomFieldExistingOptionGroup() { $this->assertEquals($optionGroupID, 3); } + /** + * Test adding an optionGroup to an existing field doesn't cause a fatal error. + * + * (this was happening due to a check running despite no existing option_group_id) + */ + public function testUpdateCustomFieldAddOptionGroup() { + $customGroup = $this->customGroupCreate(['extends' => 'Organization', 'title' => 'test_group']); + $params = [ + 'custom_group_id' => $customGroup['id'], + 'label' => 'Organization Gender', + 'html_type' => 'Text', + 'data_type' => 'Int', + ]; + + $customField = $this->callAPISuccess('custom_field', 'create', $params); + $this->callAPISuccess('CustomField', 'create', [ + 'option_group_id' => 3, + 'id' => $customField['id'], + 'html_type' => 'Select', + ]); + } + /** * Test custom field get works & return param works */