auto_load_location-1.0.0/auto_load_location.module
auto_load_location.module
<?php
use Drupal\auto_load_location\Controller\Permission;
use Drupal\auto_load_location\Controller\Address;
use Drupal\Component\Serialization\Json;
/**
* @file
* Autoloading Address and Geolocation data from one to another content type.
*/
/**
* Implements hook_preprocess_page().
*/
function auto_load_location_preprocess_page(&$variables) {
// Check if a user has permission
$permission = new Permission();
$has_permission = $permission->getPermission();
if ($has_permission) {
$route_match = \Drupal::routeMatch();
$route = $route_match->getRouteName();
if ($route == 'node.add' ||
$route == 'entity.node.edit_form') {
// Add css/js library to node form page
$variables['#attached']['library'][] = 'auto_load_location/auto_load_location_library';
// Pass entity reference field name variable into drupalSettings
$location_field_name = \Drupal::config('auto_load_location.settings')->get('entity_reference_field_name');
$variables['#attached']['drupalSettings']['location_field_name'] = $location_field_name;
}
}
}
/**
* Implements hook_form_alter().
*/
function auto_load_location_form_alter(&$form, &$form_state, $form_id) {
$route_match = \Drupal::routeMatch();
$route_name = $route_match->getRouteName();
$node = $route_match->getParameter('node');
// Enabled content types and field name
$enabled_content_types = \Drupal::config('auto_load_location.settings')->get('enabled_content_types');
$content_types = (!empty($enabled_content_types) ? array_filter($enabled_content_types) : [] );
$location_field_name_config = \Drupal::config('auto_load_location.settings')->get('entity_reference_field_name');
$enabled_autoload_edit = \Drupal::config('auto_load_location.settings')->get('enabled_autoload_edit');
// Content type of current node which the form is attached to.
$destination_content_type = (isset($form["#process"][1][0]) ? $form["#process"][1][0]->getTargetBundle() : '');
if ( isset($destination_content_type) && !empty($destination_content_type) && in_array($destination_content_type, $content_types) ) {
// Add autoload buttons for entity reference field.
foreach ($form as $field_name => $field) {
if (preg_match('/field_/', $field_name, $matches)) {
if(isset($form[$field_name]["widget"][0]["target_id"]) && $form[$field_name]["widget"][0]["target_id"]["#type"] == 'entity_autocomplete') {
// Add load address button to reference field.
$load_address_label = t('Load address');
// Add load lat/lon button to reference field.
$load_geolocation_label = t('Load geolocation');
if (!empty($location_field_name_config) && $location_field_name_config == $field_name) {
$form[$field_name]["#suffix"] = '<div id="load-address"><span class="button autoloading-button" id="load-address-button">'.$load_address_label.'</span></div><div id="load-geolocation"><span class="button autoloading-button" id="load-geolocation-button">'.$load_geolocation_label.'</span></div>';
}
break;
}
}
}
// Stop if autoload on node edit is not enabled.
if (!$enabled_autoload_edit) {
return false;
}
// Get first entity reference location field name
if ($node) {
foreach ($node->getFields() as $name => $field) {
$field_name = $field->getName();
if (preg_match('/field_/', $field_name, $matches)) {
// Make sure the saved field name in configuration is the same as called entity reference field.
if ($field_name == $location_field_name_config && $field->getFieldDefinition()->getType() == "entity_reference") {
$location_field_name = $field_name;
break;
}
}
}
}
// Add load button and set default value for fields on node edit.
if ( isset($location_field_name) &&
($form_id == 'node_'.$destination_content_type.'_edit_form' ||
$form_id == 'node_'.$destination_content_type.'_form') ) {
foreach ($form as $field_name => $field) {
if (preg_match('/field_/', $field_name, $matches)) {
// Determine if address field exists.
if (isset($field["widget"][0]["address"])) {
if ($form_state->getUserInput() || $form_id == 'node_'.$destination_content_type.'_edit_form') {
// Get address info.
$address_info = auto_load_location_get_address_info('address', $node, $form_id, $form_state, $destination_content_type, $location_field_name);
// Populate address info for target field.
if (isset($address_info['location_country_code'])) {
$form[$field_name]["widget"][0]["address"]["#default_value"]["country_code"] = $address_info['location_country_code'];
$form[$field_name]["widget"][0]["address"]["#default_value"]["administrative_area"] = $address_info['location_administrative_area'];
$form[$field_name]["widget"][0]["address"]["#default_value"]["locality"] = $address_info['location_locality'];
$form[$field_name]["widget"][0]["address"]["#default_value"]["dependent_locality"] = $address_info['location_dependent_locality'];
$form[$field_name]["widget"][0]["address"]["#default_value"]["postal_code"] = $address_info['location_postal_code'];
$form[$field_name]["widget"][0]["address"]["#default_value"]["sorting_code"] = $address_info['location_sorting_code'];
$form[$field_name]["widget"][0]["address"]["#default_value"]["address_line1"] = $address_info['location_address_line1'];
$form[$field_name]["widget"][0]["address"]["#default_value"]["address_line2"] = $address_info['location_address_line2'];
$form[$field_name]["widget"][0]["address"]["#default_value"]["organization"] = $address_info['location_organization'];
$form[$field_name]["widget"][0]["address"]["#default_value"]["given_name"] = $address_info['location_given_name'];
$form[$field_name]["widget"][0]["address"]["#default_value"]["additional_name"] = $address_info['location_additional_name'];
$form[$field_name]["widget"][0]["address"]["#default_value"]["family_name"] = $address_info['location_family_name'];
}
}
}
// Determine if geolocation field exists.
else if (isset($field["widget"][0]["lat"])) {
if ($form_state->getUserInput() || $form_id == 'node_'.$destination_content_type.'_edit_form') {
// Get geolocation info.
$address_info = auto_load_location_get_address_info('latlon', $node, $form_id, $form_state, $destination_content_type, $location_field_name);
// Populate geolocation info for target field.
if (isset($address_info['location_lat'])) {
$form[$field_name]["widget"][0]["lat"]["#default_value"] = $address_info['location_lat'];
$form[$field_name]["widget"][0]["lng"]["#default_value"] = $address_info['location_lon'];
}
}
}
}
}
}
}
}
/**
* Get address for form.
*/
function auto_load_location_get_address_info($type, $node, $form_id, $form_state, $destination_content_type, $location_field_name) {
if ($form_id == 'node_'.$destination_content_type.'_form') {
$referenced_node = $form_state->getUserInput()[$location_field_name][0]["target_id"];
preg_match('/^(.*?)\(([0-9]+)\)$/', $referenced_node, $matches);
$referenced_id = $matches[2];
}
else if ($node->get($location_field_name)->getValue()[0]["target_id"]) {
$referenced_id = $node->get($location_field_name)->getValue()[0]["target_id"];
}
else {
return false;
}
// Call address class to get location data.
$address = new Address();
$address_json = ($type == 'address' ? $address->getAddress($referenced_id) : $address->getLatLon($referenced_id));
$address_info = Json::decode($address_json->getContent());
return $address_info;
}