Last active
August 4, 2023 13:12
-
-
Save herbdool/5887dbdbb8a503b17c52d657f8854a92 to your computer and use it in GitHub Desktop.
Convert Location module data to Address, Telephone and Email
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
<?php | |
/** | |
* Convert Location to Address, Telephone and Email | |
* | |
* bee php-script convert-location.php | |
* | |
* @todo add batching. | |
* | |
* - Requires using bee commandline tools in Backdrop: https://github.com/backdrop-contrib/bee | |
* - Requires setting the mapping in the script below. | |
* The format is keyed by the entity bundle and requires the entity type to be | |
* listed and the old location and new fields. | |
* - It current assumes there is only one location field per entity bundle. | |
* - The target fields must be manually created before running this script. | |
* - Works with nodes, taxonomy terms, paragraphs items, and field collection | |
* items, but can be extended to other entities. | |
* - (optional) Set overrides for country and state_province. | |
*/ | |
global $entity_mapping; | |
global $overrides; | |
$entity_mapping = array( | |
'medical_specialist' => array( | |
'entity_type' => 'node', | |
'location_field' => 'specialist_location', | |
'address_field' => 'specialist_address', | |
'phone_field' => 'specialist_phone', | |
'fax_field' => 'specialist_fax', | |
'email_field' => 'specialist_email', | |
), | |
'house_location' => array( | |
'entity_type' => 'taxonomy_term', | |
'location_field' => 'house_location', | |
'address_field' => 'house_address', | |
), | |
'next_of_kin' => array( | |
'entity_type' => 'paragraphs_item', | |
'location_field' => 'nofk_location', | |
'address_field' => 'nofk_address', | |
'phone_field' => 'nofk_phone', | |
), | |
); | |
$overrides = [ | |
'country' => 'CA', // 2-letter ISO code | |
'state_province' => 'ON', // 2-letter ISO code | |
]; | |
/** | |
* DO NOT EDIT BELOW. | |
*/ | |
$node_types = $vocabulary = $paragraph_types = $field_collection_types = []; | |
foreach ($entity_mapping as $bundle => $item) { | |
switch ($item['entity_type']) { | |
case 'node': | |
$node_types[] = $bundle; | |
break; | |
case 'taxonomy_term': | |
$vocabulary[] = $bundle; | |
break; | |
case 'paragraphs_item': | |
$paragraph_types[] = $bundle; | |
break; | |
case 'field_collection_item': | |
$field_collection_types[] = $bundle; | |
break; | |
} | |
} | |
$success = []; | |
// Nodes | |
if (module_exists('node')) { | |
$nids = []; | |
$query = db_select('node') | |
->fields('node', array('nid')) | |
->condition('type', $node_types, 'IN'); | |
$nids = $query->execute()->fetchCol(); | |
foreach($nids as $nid) { | |
$node = node_load($nid); | |
if ($node) { | |
$success[] = convert_location_data($node); | |
} | |
} | |
} | |
// Taxonomy terms | |
if (module_exists('taxonomy')) { | |
$tids = []; | |
$query = db_select('taxonomy_term_data', 'ttd') | |
->fields('ttd', array('tid')) | |
->condition('vocabulary', $vocabulary, 'IN'); | |
$tids = $query->execute()->fetchCol(); | |
foreach($tids as $tid) { | |
$term = entity_load('taxonomy_term', $tid); | |
if ($term) { | |
$success[] = convert_location_data($term); | |
} | |
} | |
} | |
// Paragraph items | |
if (module_exists('paragraphs')) { | |
$p_items = []; | |
$query = db_select('paragraphs_item', 'pi') | |
->fields('pi', array('item_id', 'field_name')) | |
->condition('bundle', $paragraph_types, 'IN'); | |
$p_items = $query->execute()->fetchAll(); | |
foreach($p_items as $p_item) { | |
$paragraphs_item = paragraphs_item_load($p_item->item_id); | |
if ($paragraphs_item) { | |
$host = $paragraphs_item->hostEntity(); | |
} | |
if ($paragraphs_item && $host) { | |
$success[] = convert_location_data($paragraphs_item); | |
} | |
} | |
} | |
// Field Collection items | |
if (module_exists('field_collection')) { | |
$fc_items = []; | |
$query = db_select('field_collection_item', 'fci') | |
->fields('fci', array('item_id', 'field_name')) | |
->condition('bundle', $field_collection_types, 'IN'); | |
$fc_items = $query->execute()->fetchAll(); | |
foreach($fc_items as $fc_item) { | |
$field_collection_item = field_collection_item_load($fc_item->item_id); | |
if ($field_collection_item) { | |
$host = $field_collection_item->hostEntity(); | |
} | |
if ($field_collection_item && $host) { | |
$success[] = convert_location_data($field_collection_item); | |
} | |
} | |
} | |
echo "Number of locations converted: " . count($success) . PHP_EOL; | |
/** | |
* Convert location data | |
* | |
* @param EntityInterface $entity | |
* @return bool | |
*/ | |
function convert_location_data($entity) { | |
$items = fetch_location_data($entity); | |
global $entity_mapping; | |
global $overrides; | |
$address_field = isset($entity_mapping[$entity->bundle()]['address_field']) ? 'field_' . $entity_mapping[$entity->bundle()]['address_field'] : NULL; | |
$phone_field = isset($entity_mapping[$entity->bundle()]['phone_field']) ? 'field_' . $entity_mapping[$entity->bundle()]['phone_field'] : NULL; | |
$fax_field = isset($entity_mapping[$entity->bundle()]['fax_field']) ? 'field_' . $entity_mapping[$entity->bundle()]['fax_field'] : NULL; | |
$email_field = isset($entity_mapping[$entity->bundle()]['email_field']) ? 'field_' . $entity_mapping[$entity->bundle()]['email_field'] : NULL; | |
foreach ($items as $item) { | |
if ($address_field) { | |
$entity->{$address_field}[$item->language][$item->delta]['country'] = $overrides['country']; | |
$entity->{$address_field}[$item->language][$item->delta]['administrative_area'] = $overrides['state_province']; | |
$entity->{$address_field}[$item->language][$item->delta]['locality'] = $item->city; | |
$entity->{$address_field}[$item->language][$item->delta]['postal_code'] = $item->postal_code; | |
$entity->{$address_field}[$item->language][$item->delta]['thoroughfare'] = $item->street; | |
$entity->{$address_field}[$item->language][$item->delta]['name_line'] = $item->name; | |
$entity->{$address_field}[$item->language][$item->delta]['premise'] = $item->additional; | |
} | |
if ($phone_field && !empty($item->phone)) { | |
$entity->{$phone_field}[$item->language][$item->delta]['value'] = $item->phone; | |
} | |
if ($fax_field && !empty($item->fax)) { | |
$entity->{$fax_field}[$item->language][$item->delta]['value'] = $item->fax; | |
} | |
if ($email_field && !empty($item->email)) { | |
$entity->{$email_field}[$item->language][$item->delta]['email'] = $item->email; | |
} | |
$entity->save(); | |
} | |
} | |
/** | |
* Fetch location data | |
*/ | |
function fetch_location_data($entity) { | |
global $entity_mapping; | |
// Query db directly since assume location module not enabled. | |
$location_field = $entity_mapping[$entity->bundle()]['location_field']; | |
$query = db_select('field_data_field_' . $location_field, 'f'); | |
$query->leftJoin('location', 'l', 'l.lid = f.field_' . $location_field . '_lid'); | |
$query->leftJoin('location_phone', 'lp', 'lp.lid = f.field_' . $location_field . '_lid'); | |
$query->leftJoin('location_fax', 'lf', 'lf.lid = f.field_' . $location_field . '_lid'); | |
$query->leftJoin('location_email', 'le', 'le.lid = f.field_' . $location_field . '_lid'); | |
$query->fields('f', ['entity_type', 'bundle', 'deleted', 'entity_id', 'revision_id', 'language', 'delta']); | |
$query->fields('l', ['name', 'street', 'additional', 'city', 'province', 'postal_code', 'country']); | |
$query->fields('lp', ['phone']); | |
$query->fields('lf', ['fax']); | |
$query->fields('le', ['email']); | |
$query->condition('f.entity_id', $entity->id()); | |
$items = $query->execute()->fetchAll(); | |
return $items; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment