Created
March 14, 2019 18:44
-
-
Save andrewadcock/0fa1067b0d5c15118b124fa815127d63 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Implements hook_webform_submission_presave(). | |
* | |
* If changes to establishments are made during AAP, save them to node before | |
* form is complete | |
* | |
* @param WebformSubmissionInterface $webform_submission | |
* | |
* @throws \Drupal\Core\Entity\EntityStorageException | |
*/ | |
function aavi_establishments_webform_submission_presave(WebformSubmissionInterface $webform_submission) { | |
// If form is not AAP, skip this function | |
if ($webform_submission->getWebform()->id() != "affirmative_action_plan") { | |
return; | |
} | |
// Get altered data | |
$altered = $webform_submission->getData(); | |
// Get establishment node | |
$establishment_nid = $altered['establishment']; | |
// Get node object | |
$node = Node::load($establishment_nid); | |
// Assume no changes have been made | |
$change_occurred = FALSE; | |
foreach ($altered as $key => $field) { | |
// Get field name | |
$transposed_field_name = _establishmentWebformFieldMap($key); | |
// If there is a not a match, do not bother to process | |
if (!$transposed_field_name) { | |
continue; | |
} | |
// Determine is there is a matching field in the node | |
$match = $node->hasField($transposed_field_name); | |
if ($key == "establishment_mailing_address" || $key == "establishment_physical_address") { | |
$address_array = _buildAddressArray($altered[$key]); | |
try { | |
$node->set($transposed_field_name, $address_array); | |
$change_occurred = TRUE; | |
} catch (Exception $e) { | |
Drupal::logger('webform')->error($e->getMessage()); | |
} | |
} elseif ($match) { | |
if (gettype($field) == "array") { | |
if (array_diff($field, $altered[$key])) { | |
$change_occurred = TRUE; | |
$node->set($transposed_field_name, $altered[$key]); | |
} | |
} | |
elseif (gettype($field) == "string") { | |
if ($field != $altered[$key]) { | |
$change_occurred = TRUE; | |
$node->set($transposed_field_name, $altered[$key]); | |
} | |
} | |
} | |
} | |
if ($change_occurred) { | |
$node->setNewRevision(FALSE); | |
$node->save(); | |
} | |
} | |
/** | |
* Map fields between Address field and webform field | |
* | |
* @param string $key | |
* | |
* @return bool|mixed | |
*/ | |
function _establishmentWebformFieldMap($key = '') { | |
$map = [ | |
'establishment_name' => 'field_display_title', | |
'establishment_number' => 'field_establishment_id', | |
'establishment_duns' => 'field_duns', | |
'establishment_naics' => 'field_naics_code_term', | |
'establishment_status_code' => 'field_status_code', | |
'establishment_total_10' => 'field_total10', | |
'establishment_physical_address' => 'field_physical_address', | |
'establishment_mailing_address' => 'field_mailing_address', | |
]; | |
if (isset($key) && array_key_exists($key, $map)) { | |
return $map[$key]; | |
} | |
else { | |
return FALSE; | |
} | |
} | |
/** | |
* Return array that matches Address field keys | |
* | |
* @param $address_fields | |
* | |
* @return array | |
*/ | |
function _buildAddressArray($address_fields) { | |
return [ | |
'additional_name' => (isset($address_fields['additional_name']) ? $address_fields['additional_name'] : NULL), | |
'address_line1' => (isset($address_fields['address']) ? $address_fields['address'] : NULL), | |
'address_line2' => (isset($address_fields['address_2']) ? $address_fields['address_2'] : NULL), | |
'administrative_area' => (isset($address_fields['state_province']) ? $address_fields['state_province'] : NULL), | |
'country_code' => 'US', | |
'dependent_locality' => (isset($address_fields['dependent_locality']) ? $address_fields['dependent_locality'] : NULL), | |
'family_name' => (isset($address_fields['family_name']) ? $address_fields['family_name'] : NULL), | |
'given_name' => (isset($address_fields['given_name']) ? $address_fields['given_name'] : NULL), | |
'locality' => (isset($address_fields['city']) ? $address_fields['city'] : NULL), | |
'organization' => (isset($address_fields['organization']) ? $address_fields['organization'] : NULL), | |
'postal_code' => (isset($address_fields['postal_code']) ? $address_fields['postal_code'] : NULL), | |
'sorting_code' => (isset($address_fields['sorting_code']) ? $address_fields['sorting_code'] : NULL), | |
]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment