protected function WebformCivicrmPostProcess::reorderLocationValues in Webform CiviCRM Integration 8.5
Reorder submitted location values according to existing location values
Parameters
array $submittedLocationValues:
array $existingLocationValues:
string $entity:
Return value
array
1 call to WebformCivicrmPostProcess::reorderLocationValues()
- WebformCivicrmPostProcess::saveContactLocation in src/
WebformCivicrmPostProcess.php - Save location data for a contact
File
- src/
WebformCivicrmPostProcess.php, line 2710 - Front-end form validation and post-processing.
Class
Namespace
Drupal\webform_civicrmCode
protected function reorderLocationValues($submittedLocationValues, $existingLocationValues, $entity) {
$reorderedArray = [];
$index = 1;
$entityTypeIdIndex = $entity . '_type_id';
$entity = $entity == 'website' ? 'url' : $entity;
// for website only
$utils = \Drupal::service('webform_civicrm.utils');
foreach ($existingLocationValues as $eValue) {
$existingLocationTypeId = $entity != 'url' ? $eValue['location_type_id'] : NULL;
$existingEntityTypeId = isset($eValue[$entityTypeIdIndex]) ? $eValue[$entityTypeIdIndex] : NULL;
if (!empty($existingEntityTypeId)) {
$reorderedArray[$index][$entityTypeIdIndex] = $existingEntityTypeId;
}
elseif (!empty($existingLocationTypeId)) {
$reorderedArray[$index]['location_type_id'] = $existingLocationTypeId;
}
if (!empty($eValue[$entity])) {
$reorderedArray[$index][$entity] = $eValue[$entity];
}
// address field contain many sub fields and should be handled differently
if ($entity != 'address') {
$submittedLocationValues = self::unsetEmptyValueIndexes($submittedLocationValues, $entity);
$reorderedArray[$index][$entity] = wf_crm_aval($eValue, $entity);
}
else {
foreach ($utils
->wf_crm_address_fields() as $field) {
if ($field != 'location_type_id') {
$reorderedArray[$index][$field] = isset($eValue[$field]) ? $eValue[$field] : '';
}
}
}
foreach ($submittedLocationValues as $key => $sValue) {
$sLocationTypeId = isset($sValue['location_type_id']) ? $sValue['location_type_id'] : NULL;
$sEntityTypeId = isset($sValue[$entityTypeIdIndex]) ? $sValue[$entityTypeIdIndex] : NULL;
if ($existingLocationTypeId == $sLocationTypeId && empty($sEntityTypeId) || $existingEntityTypeId == $sEntityTypeId && empty($sLocationTypeId) || $existingLocationTypeId == $sLocationTypeId && $existingEntityTypeId == $sEntityTypeId) {
// address field contain many sub fields and should be handled differently
if ($entity != 'address') {
$reorderedArray[$index] = $sValue;
unset($submittedLocationValues[$key]);
break;
}
else {
foreach ($utils
->wf_crm_address_fields() as $field) {
if (isset($sValue[$field]) && $field != 'location_type_id') {
$reorderedArray[$index][$field] = $sValue[$field];
}
}
}
unset($submittedLocationValues[$key]);
}
}
$index++;
}
// handle remaining values
if (!empty($submittedLocationValues)) {
// cannot use array_push, array_merge because preserving array keys is important
foreach ($submittedLocationValues as $sValue) {
$reorderedArray[] = $sValue;
}
}
return $reorderedArray;
}