diff --git a/processors/relationship/class-relationship-processor.php b/processors/relationship/class-relationship-processor.php
index 5c2aa81..438f026 100644
--- a/processors/relationship/class-relationship-processor.php
+++ b/processors/relationship/class-relationship-processor.php
@@ -72,27 +72,31 @@ public function pre_processor( $config, $form ) {
// cfc transient object
$transient = $this->plugin->transient->get();
- $relationship = civicrm_api3( 'Relationship', 'get', [
- 'sequential' => 1,
+ $identifying_params = [
'contact_id_a' => $transient->contacts->{'cid_'.$config['contact_a']},
'contact_id_b' => $transient->contacts->{'cid_'.$config['contact_b']},
'relationship_type_id' => $config['relationship_type'],
- ] );
+ ];
- if ( $relationship['count'] ) {
- return;
+ $relationship = civicrm_api3( 'Relationship', 'get', [ 'sequential' => 1 ] + $identifying_params );
+
+ // Get form values
+ $form_values = []; // Must always be an array, or adding another array will fail.
+ $form_values = $this->plugin->helper->map_fields_to_processor( $config, $form, $form_values );
+
+ if ( $relationship['count'] === 0 ) {
+ $form_values += $identifying_params;
+ } elseif ( $relationship['count'] === 1 ) {
+ $form_values['id'] = $relationship['values'][0]['id']; // Relationship ID
} else {
- try {
- $create_relationship = civicrm_api3( 'Relationship', 'create', [
- 'sequential' => 1,
- 'contact_id_a' => $transient->contacts->{'cid_'.$config['contact_a']},
- 'contact_id_b' => $transient->contacts->{'cid_'.$config['contact_b']},
- 'relationship_type_id' => $config['relationship_type'],
- ] );
- } catch ( CiviCRM_API3_Exception $e ) {
- $error = $e->getMessage() . '
' . $e->getTraceAsString() . ''; - return [ 'note' => $error, 'type' => 'error' ]; - } + return; + } + + try { + $create_relationship = civicrm_api3( 'Relationship', 'create', $form_values ); + } catch ( CiviCRM_API3_Exception $e ) { + $error = $e->getMessage() . '
' . $e->getTraceAsString() . ''; + return [ 'note' => $error, 'type' => 'error' ]; } } diff --git a/processors/relationship/relationship_config.php b/processors/relationship/relationship_config.php index 24ae631..3f384ea 100644 --- a/processors/relationship/relationship_config.php +++ b/processors/relationship/relationship_config.php @@ -6,6 +6,10 @@ 'options' => [ 'limit' => 0 ], ] ); +$relationshipFields = civicrm_api3( 'Relationship', 'getfields', [] ); + +$fields = [ 'start_date', 'end_date' ]; + ?>