diff --git a/config/install/field.field.node.uw_ct_service.field_uw_service_location_coord.yml b/config/install/field.field.node.uw_ct_service.field_uw_service_location_coord.yml index 2fd4ed339a2320853b5361a5c24845025ebf62e6..b6670c31b144a951b7ebefea3147f2a4c0b85c95 100644 --- a/config/install/field.field.node.uw_ct_service.field_uw_service_location_coord.yml +++ b/config/install/field.field.node.uw_ct_service.field_uw_service_location_coord.yml @@ -11,7 +11,7 @@ field_name: field_uw_service_location_coord entity_type: node bundle: uw_ct_service label: 'Location coordinates' -description: 'If the address is not sufficient for locating the event, enter its coordinates here. To remove the coordinates, empty or set to zero either latitude or longitude.' +description: 'If the address is not sufficient for locating the service, enter its coordinates here. To remove the coordinates, empty or set to zero either latitude or longitude.' required: false translatable: false default_value: { } diff --git a/js/location_autofill.js b/js/location_autofill.js index 54ac6c671308209af30597006d05fc52fbdb4134..62b97b1a6a6cf0fd91b5a466fb27e48a7e16e0d1 100644 --- a/js/location_autofill.js +++ b/js/location_autofill.js @@ -1,28 +1,28 @@ /** * @file - * Javascript for event location presets menu. + * Javascript for service location presets menu. */ (function ($, Drupal, drupalSettings) { 'use strict'; - Drupal.behaviors.uw_ct_event_location_autofill = { + Drupal.behaviors.uw_ct_service_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()]; + var location = drupalSettings.uwCtService[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(); + $('input#lat-edit-field-uw-service-location-coord-0-value').val(location.latitude); + $('input#lon-edit-field-uw-service-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'); + var select_country = $('#edit-field-uw-service-location-0 select.country'); if (select_country.val() !== location.country_code) { select_country.val(location.country_code).change(); } @@ -47,10 +47,10 @@ if (counter > 50) { clearInterval(checkExist); } - else if (!$('.ajax-progress-throbber').length && $('#edit-field-uw-event-location-address-0 input.organization').length) { + else if (!$('.ajax-progress-throbber').length && $('#edit-field-uw-service-location-0 input.organization').length) { clearInterval(checkExist); for (var key in address_fields) { - var element = $('#edit-field-uw-event-location-address-0 ' + address_fields[key]); + var element = $('#edit-field-uw-service-location-0 ' + address_fields[key]); if (element.val() !== location[key]) { element.val(location[key]).change(); } diff --git a/js/uw_ct_service_edit.js b/js/uw_ct_service_edit.js new file mode 100644 index 0000000000000000000000000000000000000000..421bc9bd4ca200331ee43bdff4e780b99cba13bb --- /dev/null +++ b/js/uw_ct_service_edit.js @@ -0,0 +1,27 @@ +/** + * @file + * Javascript for editing uw_ct_service. + */ + +(function ($, Drupal, drupalSettings) { + + 'use strict'; + + Drupal.behaviors.uw_ct_service_edit = { + attach: function (context, drupalSettings) { + + // Marker at 0,0 means it is blank. Center the map on UW main campus. + $(document).on('geofieldMapInit', function (e, mapid) { + // Field containing the service location. + let service_location_mapid = 'map-edit-field-uw-service-location-coord-0-value'; + if (mapid === service_location_mapid && drupalSettings['geofield_map'][mapid].lat === 0 && drupalSettings['geofield_map'][mapid].lng === 0) { + // Get coordinates of the center of UW main campus. + let position = {lat: 43.471, lng: -80.542}; + // Center the map. + Drupal.geoFieldMap.mapSetCenter(mapid, position); + } + }); + } + } + +})(jQuery, Drupal, drupalSettings); diff --git a/uw_ct_service.libraries.yml b/uw_ct_service.libraries.yml new file mode 100644 index 0000000000000000000000000000000000000000..df846e9f6e4e601203a6946eb542d537de17428f --- /dev/null +++ b/uw_ct_service.libraries.yml @@ -0,0 +1,6 @@ +uw_ct_service_edit: + js: + js/uw_ct_service_edit.js: {} +location_autofill: + js: + js/location_autofill.js: {} diff --git a/uw_ct_service.module b/uw_ct_service.module index 06920724f8aa3a12b85abfab9ebd1957f97b0535..3bfaa87ffc8bd9c0a5e2d0dd8d4b0a0b6161ed9c 100644 --- a/uw_ct_service.module +++ b/uw_ct_service.module @@ -7,6 +7,16 @@ use Drupal\Core\Form\FormStateInterface; +/** + * Implements hook_geofield_map_latlon_element_alter(). + */ +function uw_ct_service_geofield_map_latlon_element_alter(array &$map_settings, array &$complete_form, array &$form_state_values) { + // Library for editing uw_ct_service. + if ($map_settings['id'] === 'edit-field-uw-service-location-coord-0-value') { + $complete_form['#attached']['library'][] = 'uw_ct_service/uw_ct_service_edit'; + } +} + /** * Load data about UW locations from a JSON file. * @@ -95,3 +105,15 @@ function uw_ct_service_form_node_uw_ct_service_form_alter(array &$form, FormStat $form['#attached']['library'][] = 'uw_ct_service/location_autofill'; $form['#attached']['drupalSettings']['uwCtService'] = _uw_ct_service_load_locations(); } + +/** + * Implements hook_form_alter(). + */ +function uw_ct_service_form_alter(array &$form, FormStateInterface $form_state, string $form_id): void { + switch ($form_id) { + case 'node_uw_ct_service_edit_form': + case 'node_uw_ct_service_quick_node_clone_form': + uw_ct_service_form_node_uw_ct_service_form_alter($form, $form_state, $form_id); + break; + } +}