From 6435d60aeabb6c8473c85d98a328b9ad58155577 Mon Sep 17 00:00:00 2001 From: l26yan <l26yan@uwaterloo.ca> Date: Tue, 5 Oct 2021 10:55:58 -0400 Subject: [PATCH] ISTWCMS-5124 Add locations pre-fill menu to service edit and clone pages --- ...ervice.field_uw_service_location_coord.yml | 2 +- js/location_autofill.js | 16 +++++------ js/uw_ct_service_edit.js | 27 +++++++++++++++++++ uw_ct_service.libraries.yml | 6 +++++ uw_ct_service.module | 22 +++++++++++++++ 5 files changed, 64 insertions(+), 9 deletions(-) create mode 100644 js/uw_ct_service_edit.js create mode 100644 uw_ct_service.libraries.yml 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 2fd4ed3..b6670c3 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 54ac6c6..62b97b1 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 0000000..421bc9b --- /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 0000000..df846e9 --- /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 0692072..3bfaa87 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; + } +} -- GitLab