private function WebformCivicrmPostProcess::addOrRemoveMultivaluedData in Webform CiviCRM Integration 8.5
Handle adding/removing multivalued data for a contact/activity/etc. Currently used only for groups and tags, but written with expansion in mind.
Parameters
$data_type: 'group' or 'tag'
$entity_type: Parent entity: 'contact' etc.
$id: Entity id
$add: Groups/tags to add
array $remove: Groups/tags to remove
2 calls to WebformCivicrmPostProcess::addOrRemoveMultivaluedData()
- WebformCivicrmPostProcess::handleEntityTags in src/
WebformCivicrmPostProcess.php - Handle adding/updating tags for entities (cases, activity)
- WebformCivicrmPostProcess::saveGroupsAndTags in src/
WebformCivicrmPostProcess.php - Save groups and tags for a contact
File
- src/
WebformCivicrmPostProcess.php, line 946 - Front-end form validation and post-processing.
Class
Namespace
Drupal\webform_civicrmCode
private function addOrRemoveMultivaluedData($data_type, $entity_type, $id, $add, $remove = []) {
$utils = \Drupal::service('webform_civicrm.utils');
$confirmations_sent = $existing = $params = [];
$add = array_combine($add, $add);
static $mailing_lists = [];
switch ($data_type) {
case 'group':
$api = 'group_contact';
$params['method'] = substr(t('Webform'), 0, 8);
break;
case 'tag':
$api = 'EntityTag';
break;
default:
$api = $data_type;
}
if (!empty($add) || !empty($remove)) {
// Retrieve current records for this entity
if ($api == 'EntityTag') {
$params['entity_id'] = $id;
$params['entity_table'] = 'civicrm_' . $entity_type;
}
elseif ($api == 'group_contact' && $entity_type == 'contact') {
$params['contact_id'] = $id;
}
else {
$params['entity_id'] = $id;
$params['entity_type'] = 'civicrm_' . $entity_type;
}
$fetch = $utils
->wf_civicrm_api($api, 'get', $params);
foreach (wf_crm_aval($fetch, 'values', []) as $i) {
$existing[] = $i[$data_type . '_id'];
unset($add[$i[$data_type . '_id']]);
}
foreach ($remove as $i => $name) {
if (!in_array($i, $existing)) {
unset($remove[$i]);
}
}
}
if (!empty($add)) {
// Prepare for sending subscription confirmations
if ($data_type == 'group' && !empty($this->settings['confirm_subscription'])) {
// Retrieve this contact's primary email address and perform error-checking
$result = $utils
->wf_civicrm_api('email', 'get', [
'contact_id' => $id,
'options' => [
'sort' => 'is_primary DESC',
],
]);
if (!empty($result['values'])) {
foreach ($result['values'] as $value) {
if (($value['is_primary'] || empty($email)) && strpos($value['email'], '@')) {
$email = $value['email'];
}
}
$mailer_params = [
'contact_id' => $id,
'email' => $email,
];
if (empty($mailing_lists)) {
$mailing_lists = $utils
->wf_crm_apivalues('group', 'get', [
'visibility' => 'Public Pages',
'group_type' => 2,
], 'title');
}
}
}
foreach ($add as $a) {
$params[$data_type . '_id'] = $mailer_params['group_id'] = $a;
if ($data_type == 'group' && isset($mailing_lists[$a]) && !empty($email)) {
$result = $utils
->wf_civicrm_api('mailing_event_subscribe', 'create', $mailer_params);
if (empty($result['is_error'])) {
$confirmations_sent[] = Html::escape($mailing_lists[$a]);
}
else {
$utils
->wf_civicrm_api($api, 'create', $params);
}
}
else {
$utils
->wf_civicrm_api($api, 'create', $params);
}
}
if ($confirmations_sent) {
\Drupal::messenger()
->addStatus(t('A message has been sent to %email to confirm subscription to :group.', [
'%email' => $email,
':group' => '' . implode('' . t('and') . '', $confirmations_sent) . '',
]));
}
}
// Remove data from entity
foreach ($remove as $a => $name) {
$params[$data_type . '_id'] = $a;
$utils
->wf_civicrm_api($api, 'delete', $params);
}
if (!empty($remove) && $data_type == 'group') {
$display_name = $utils
->wf_civicrm_api('contact', 'get', [
'contact_id' => $id,
'return.display_name' => 1,
]);
$display_name = wf_crm_aval($display_name, "values:{$id}:display_name", t('Contact'));
\Drupal::messenger()
->addStatus(t('%contact has been removed from @group.', [
'%contact' => $display_name,
'@group' => '<em>' . implode('</em> ' . t('and') . ' <em>', $remove) . '</em>',
]));
}
}