You are here

private function WebformCivicrmPostProcess::saveContactRefs in Webform CiviCRM Integration 8.5

Save custom contact reference created during the current submission of the webform.

Parameters

array $params:

int $cid:

1 call to WebformCivicrmPostProcess::saveContactRefs()
WebformCivicrmPostProcess::preSave in src/WebformCivicrmPostProcess.php
Process webform submission when it is about to be saved. Called by the following hook:

File

src/WebformCivicrmPostProcess.php, line 2833
Front-end form validation and post-processing.

Class

WebformCivicrmPostProcess

Namespace

Drupal\webform_civicrm

Code

private function saveContactRefs($params, $cid) : void {
  $updateParams = [
    'id' => $cid,
  ];
  $skipKeys = [];
  foreach ($params['update_contact_ref'] as $n => $refKeys) {
    foreach ($refKeys as $refKey => $val) {

      // Skip contact ref that doesn't have a valid contact ids.
      if (empty($this->ent['contact'][$val]['id'])) {
        continue;
      }
      foreach ($params['contact'] as $contactParams) {
        foreach ($contactParams as $key => $value) {
          if (strpos($key, "{$refKey}_") === 0 && !isset($updateParams[$key]) && !in_array($key, $skipKeys)) {

            //If this is an edit to an existing record, remove any matching keys that was previously set to 'create' new records.
            if (strpos($key, "{$refKey}_-") !== 0) {
              foreach ($updateParams as $k => $v) {
                if (strpos($k, "{$refKey}_-") === 0) {
                  $skipKeys[] = $k;
                  unset($updateParams[$k]);
                }
              }
            }
            $updateParams[$key] = $value;
          }
        }
      }
    }
  }
  if (count($updateParams) > 1) {
    \Drupal::service('webform_civicrm.utils')
      ->wf_civicrm_api('contact', 'create', $updateParams);
  }
}