You are here

private function WebformCivicrmPostProcess::saveGroupsAndTags in Webform CiviCRM Integration 8.5

Save groups and tags for a contact

Parameters

array $contact:

int $cid:

int $c:

1 call to WebformCivicrmPostProcess::saveGroupsAndTags()
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 910
Front-end form validation and post-processing.

Class

WebformCivicrmPostProcess

Namespace

Drupal\webform_civicrm

Code

private function saveGroupsAndTags($contact, $cid, $c) {

  // Process groups & tags
  foreach ($this->all_fields as $fid => $field) {
    list($set, $type) = explode('_', $fid, 2);
    if ($set == 'other') {
      $field_name = 'civicrm_' . $c . '_contact_1_' . $fid;
      if (!empty($contact['other'][1][$type]) || isset($this->enabled[$field_name])) {
        $add = wf_crm_aval($contact, "other:1:{$type}", []);

        // ToDo - $add should only contain the option(s) selected so unset everything else b/c addOrRemoveMultivaluedData is expecting that and we need this to handle TagSets - this is essentially a fail-safe
        foreach ($this
          ->getExposedOptions($field_name) as $k => $v) {
          if (array_key_exists($k, $add) && $add[$k] === 0) {
            unset($add[$k]);
          }
        }
        $remove = empty($this->existing_contacts[$c]) ? [] : $this
          ->getExposedOptions($field_name, $add);
        $this
          ->addOrRemoveMultivaluedData($field['table'], 'contact', $cid, $add, $remove);
      }
    }
  }
}