Commit 9f6edf6f authored by Kevin Paxman's avatar Kevin Paxman
Browse files

Merge branch 'feature/ISTWCMS-4947-ebremner-event-data' into '1.0.x'

ISTWCMS-4947: adding update hook to set country/area in location address to...

See merge request !24
parents 401cdee6 46600903
......@@ -14,21 +14,7 @@ label: 'Location address'
description: ''
required: false
translatable: false
default_value:
-
country_code: CA
langcode: ''
given_name: ''
family_name: ''
organization: ''
address_line1: ''
address_line2: ''
locality: ''
administrative_area: 'ON'
postal_code: ''
additional_name: null
sorting_code: null
dependent_locality: null
default_value: { }
default_value_callback: ''
settings:
available_countries: { }
......
......@@ -190,3 +190,59 @@ function uw_ct_event_update_8102(&$sandbox) {
}
}
}
/**
* Update location country and area to null if only components.
*/
function uw_ct_event_update_8103() {
// Get the nids and nodes for all events.
$nids = \Drupal::entityQuery('node')->condition('type', 'uw_ct_event')->execute();
$nodes = Node::loadMultiple($nids);
// The address components that need to have
// values for us not to reset country/area.
$address_components = [
'organization',
'locality',
'postal_code',
'address_line1',
'address_line2',
];
// Step through each of the nodes and check
// if we need to update the country/area.
foreach ($nodes as $node) {
// Get the address field from the node.
$address = $node->field_uw_event_location_address->getValue()[0];
// If there is an address, check if we need
// to update the country/area.
if ($address) {
// Flag to update the address. Initial set to
// TRUE, if we find that the address has a component
// then we will not update the country/area.
$address_update = TRUE;
// Step through each of the components as see if
// it has a value, and if it does set the flag
// so that we do not update the country/area.
foreach ($address_components as $address_component) {
if ($address[$address_component]) {
$address_update = FALSE;
}
}
// If we are to update country/area, then update it,
// and save the node.
if ($address_update) {
$address['country_code'] = NULL;
$address['administrative_area'] = NULL;
$node->field_uw_event_location_address->setValue($address);
$node->save();
}
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment