From 718cade13d027f0be5bd3f5f04cb5f1fa91944f3 Mon Sep 17 00:00:00 2001 From: benjamin Date: Tue, 20 Aug 2024 12:31:02 +0100 Subject: [PATCH 1/2] support EntityTag creation --- destinations/civicrm_api.inc | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/destinations/civicrm_api.inc b/destinations/civicrm_api.inc index 7979f86..b5d376f 100755 --- a/destinations/civicrm_api.inc +++ b/destinations/civicrm_api.inc @@ -151,7 +151,20 @@ class MigrateDestinationCivicrmApi extends MigrateDestinationEntity { drupal_set_message("
Params as passed to API".print_r($params,true)."
"); } migrate_instrument_start('civicrm_' . $this->entity . '_import'); - $apiresult = civicrm_api($this->entity,'create',$params); + + $apiresult = civicrm_api($this->entity, 'create', $params); + + // api3 doesn't return EntityTag ids + // to enable rollback fetch now using Api4 + // (the EntityTag record should be uniquely determined by contact_id and tag_id) + if ($this->entity === 'EntityTag' && !empty($params['contact_id']) && $apiresult['added'] ?? FALSE) { + $apiresult['id'] = Civi\Api4\EntityTag::get(FALSE) + ->addWhere('tag_id', '=', $params['tag_id']) + ->addWhere('entity_id', '=', $params['contact_id']) + ->addWhere('entity_table', '=', 'civicrm_contact') + ->execute()->single()['id']; + } + if($this->debug ==1){ drupal_set_message("
Result from API".print_r($apiresult,true)."
"); } From da3adce0ec3a11930e4f527307fba4c101cb4f7a Mon Sep 17 00:00:00 2001 From: benjamin Date: Tue, 20 Aug 2024 12:31:19 +0100 Subject: [PATCH 2/2] support EntityTag rollback --- destinations/civicrm_api.inc | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/destinations/civicrm_api.inc b/destinations/civicrm_api.inc index b5d376f..6868909 100755 --- a/destinations/civicrm_api.inc +++ b/destinations/civicrm_api.inc @@ -77,9 +77,16 @@ class MigrateDestinationCivicrmApi extends MigrateDestinationEntity { migrate_instrument_start('civicrm_contact_delete_multiple'); $this->prepareRollback($ids); foreach ($ids as $id) { - try{ - civicrm_api($this->entity, 'delete', array('version' => 3, 'id' => $id, 'skip_undelete' => 1)); - } catch (Exception $e){ + try { + if ($this->entity === 'EntityTag') { + // api3 doesnt support deleting tags directly + \Civi\Api4\EntityTag::delete(FALSE)->addWhere('id', '=', $id)->execute(); + } + else { + civicrm_api($this->entity, 'delete', ['version' => 3, 'id' => $id, 'skip_undelete' => 1]); + } + } + catch (Exception $e) { throw new MigrateException('failed to delete ' . $id); } }