Skip to content
Snippets Groups Projects
Commit 61414b8c authored by l26yan's avatar l26yan
Browse files

ISTWCMS-5124 Add locations pre-fill menu

parent ca8065d0
No related branches found
No related tags found
4 merge requests!60ISTWCMS-6095 Update maxlength settings for title, text fields and link fields...,!25ISTWCMS-5779 Click update button under...,!8Feature/istwcms 5127 ebremner services blocks gui,!4ISTWCMS-5124 Create service content type
/**
* @file
* Javascript for event location presets menu.
*/
(function ($, Drupal, drupalSettings) {
'use strict';
Drupal.behaviors.uw_ct_event_location_autofill = {
attach: function (context, drupalSettings) {
var location_select = $('select#edit-location-presets-select');
location_select.change(function () {
// Get chosen location.
var location = drupalSettings.uwCtEvent[location_select.val()];
// Do nothing when re-selecting the initial value.
if (!location) {
return;
}
// Set coordinates and re-center map.
$('input#lat-edit-field-uw-event-location-coord-0-value').val(location.latitude);
$('input#lon-edit-field-uw-event-location-coord-0-value').val(location.longitude).change();
// Set the country if it has changed.
var select_country = $('#edit-field-uw-event-location-address-0 select.country');
if (select_country.val() !== location.country_code) {
select_country.val(location.country_code).change();
}
// Update the address fields.
var address_fields = {
location_name_and_code: 'input.organization',
address: 'input.address-line1',
city: 'input.locality',
postal_code: 'input.postal-code',
province_code: 'select.administrative-area',
};
// Give time for the progress throbber to appear. Fields are updated by
// Ajax following any change in the country.
setTimeout(function () {
var counter = 0;
// Repeat until the progress throbber disappears and the address field
// appears. Update the address and clearInterval() to stop repeating.
var checkExist = setInterval(function () {
// Stop repeating after 10 seconds if Ajax gets stuck.
counter++;
if (counter > 50) {
clearInterval(checkExist);
}
else if (!$('.ajax-progress-throbber').length && $('#edit-field-uw-event-location-address-0 input.organization').length) {
clearInterval(checkExist);
for (var key in address_fields) {
var element = $('#edit-field-uw-event-location-address-0 ' + address_fields[key]);
if (element.val() !== location[key]) {
element.val(location[key]).change();
}
}
}
}, 200);
}, 500);
});
}
}
})(jQuery, Drupal, drupalSettings);
<?php
/**
* @file
* Provides configuration and settings for services.
*/
use Drupal\Core\Form\FormStateInterface;
/**
* Load data about UW locations from a JSON file.
*
* @return array[]
* The location data.
*/
function _uw_ct_service_load_locations(): array {
// Cached results of this function.
static $locations;
if ($locations) {
return $locations;
}
// Load data file.
$locations_file = file_get_contents(__DIR__ . '/uw_locations.json');
$locations_file = json_decode($locations_file, TRUE);
// Field mapping for renaming some keys.
$fields = [
'street' => 'address',
'city' => NULL,
'province' => 'province_code',
'province_name' => NULL,
'country' => 'country_code',
'country_name' => NULL,
'postal_code' => NULL,
'latitude' => NULL,
'longitude' => NULL,
];
$locations = [];
foreach ($locations_file as $file_location) {
// Parse name into code and name.
list($location_code, $location_name) = explode('-', $file_location['name'], 2);
$location_code = trim($location_code);
// Create array for this location.
$location = [
'location_code' => $location_code,
'location_name' => trim($location_name),
];
$location['location_name_and_code'] = $location['location_code'] . ' - ' . $location['location_name'];
// Copy values from file, renaming some keys.
foreach ($fields as $field_in => $field_out) {
$location[$field_out ?: $field_in] = $file_location[$field_in];
}
// Use capital letters for country codes.
$location['country_code'] = strtoupper($location['country_code']);
// Append to main array.
$locations[$location_code] = $location;
}
return $locations;
}
/**
* Return locations as a $code => $title array.
*
* @return string[]
* An array of locations with their codes as the keys and names as the values.
*/
function _uw_ct_service_get_location_options(): array {
// Cached results of this function.
static $options;
if ($options) {
return $options;
}
$options = [];
foreach (_uw_ct_service_load_locations() as $location_code => $location) {
$options[$location_code] = $location_code . ' - ' . $location['location_name'];
}
return $options;
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function uw_ct_service_form_node_uw_ct_service_form_alter(array &$form, FormStateInterface $form_state, string $form_id): void {
// Add locations pre-fill menu.
$form['group_service_location']['location_presets_select'] = [
'#title' => t('Location lookup'),
'#description' => t('Choose a location or enter location information below.'),
'#type' => 'select',
'#empty_option' => '- Choose location -',
'#options' => _uw_ct_service_get_location_options(),
];
$form['#attached']['library'][] = 'uw_ct_service/location_autofill';
$form['#attached']['drupalSettings']['uwCtService'] = _uw_ct_service_load_locations();
}
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment