From d78a9ea9f0443157adc21975baaa25af250aa552 Mon Sep 17 00:00:00 2001 From: Rich Lott / Artful Robot Date: Tue, 6 May 2025 16:40:11 +0100 Subject: [PATCH] Permit merging contacts when both have a category --- contactcats.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/contactcats.php b/contactcats.php index 18ad3a2..980c01a 100644 --- a/contactcats.php +++ b/contactcats.php @@ -55,3 +55,18 @@ function contactcats_civicrm_navigationMenu(&$menu) { ]); _contactcats_civix_navigationMenu($menu); } + +/** + * Implements hook_civicrm_merge() + * + * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_merge/ + */ +function contactcats_civicrm_merge($type, &$data, $mainId = NULL, $otherId = NULL, $tables = NULL) { + if ($type === 'sqls' && $otherId) { + // delete the 'other' contact's category; we cannot have two per contacts. + \Civi\Api4\ContactCategory::delete(FALSE) + ->addWhere('id', '=', $otherId) + ->execute(); + Civi::log()->debug("ContactCategory: deleted category for $otherId before merge into $mainId"); + } +}