Skip to content

Commit

Permalink
Merge pull request #19199 from seamuslee001/dev_core_2242
Browse files Browse the repository at this point in the history
dev/core#2242 Ensure that when a custom field is deleted any associat…
  • Loading branch information
mattwire authored Jan 7, 2021
2 parents 79caea9 + dce0a4a commit 5cf1b37
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CRM/Core/BAO/CustomField.php
Original file line number Diff line number Diff line change
Expand Up @@ -1005,12 +1005,12 @@ public static function deleteField($field) {
//not delete the option group and related option values
self::checkOptionGroup($field->option_group_id);
}

// next drop the column from the custom value table
self::createField($field, 'delete');

$field->delete();
CRM_Core_BAO_UFField::delUFField($field->id);
CRM_Core_BAO_Mapping::removeFieldFromMapping('custom_' . $field->id);
CRM_Utils_Weight::correctDuplicateWeights('CRM_Core_DAO_CustomField');

CRM_Utils_Hook::post('delete', 'CustomField', $field->id, $field);
Expand Down
10 changes: 10 additions & 0 deletions CRM/Core/BAO/Mapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -1213,4 +1213,14 @@ public static function saveMappingFields($params, $mappingId) {
}
}

/**
* Remove references to a specific field from save Mappings
* @param string $fieldName
*/
public static function removeFieldFromMapping($fieldName): void {
$mappingField = new CRM_Core_DAO_MappingField();
$mappingField->name = $fieldName;
$mappingField->delete();
}

}
5 changes: 5 additions & 0 deletions CRM/Upgrade/Incremental/sql/5.34.alpha1.mysql.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@
SELECT @country_id := id from civicrm_country where name = 'Korea, Republic of' AND iso_code = 'KR';
INSERT IGNORE INTO `civicrm_state_province` (`id`, `country_id`, `abbreviation`, `name`) VALUES
(NULL, @country_id, '50', 'Sejong');

-- Remove any references to custom fields from mapping field table that no longer exist
DELETE FROM civicrm_mapping_field
WHERE name NOT IN ( SELECT concat('custom_', id) FROM civicrm_custom_field)
AND name LIKE 'custom_%';
28 changes: 28 additions & 0 deletions tests/phpunit/api/v3/CustomFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,34 @@ public function testCustomFieldDelete() {
$this->assertAPISuccess($result);
}

/**
* Check That any associated Mapping Field Entries are also removed.
*/
public function testCustomFieldDeleteWithMappingField() {
$customGroup = $this->customGroupCreate(['extends' => 'Individual', 'title' => 'test_group']);
$customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id']]);
$this->assertNotNull($customField['id']);
$mapping = $this->callAPISuccess('Mapping', 'create', [
'name' => 'test mapping',
'mapping_type_id' => 'Export Contact',
]);
$mappingField = $this->callAPISuccess('MappingField', 'create', [
'mapping_id' => $mapping['id'],
'name' => 'custom_' . $customField['id'],
'grouping' => 1,
'column_number' => 0,
]);
$mappingFieldCheck = $this->callAPISuccess('MappingField', 'get', ['mapping_id' => $mapping['id']]);
$this->assertCount(1, $mappingFieldCheck['values']);
$params = [
'id' => $customField['id'],
];
$this->callAPISuccess('custom_field', 'delete', $params);
$mappingFieldCheck = $this->callAPISuccess('MappingField', 'get', ['mapping_id' => $mapping['id']]);
$this->assertCount(0, $mappingFieldCheck['values']);
$this->callAPISuccess('Mapping', 'delete', ['id' => $mapping['id']]);
}

/**
* Check for Option Value.
*/
Expand Down

0 comments on commit 5cf1b37

Please sign in to comment.