Problem/Motivation

I have created a webform with this module and a filed name called Pick Point

When the user submits this and the email is sent with the below token the following is returned

Token used: [webform_submission:values:pick_point]
Value printed out in email: lat { "lat": 52.355556972593305, "lng": -7.702601627895779}

Is there a way to return the lat or lng only eg using a token like

Token used maybe for lat [webform_submission:values:pick_point:lat]
Would print out: 52.355556972593305

Token used maybe for lng [webform_submission:values:pick_point:lng]
Would Print out: -7.702601627895779

Is doable with the current setup

many thanks

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

BeeOnLion created an issue. See original summary.

rajeshreeputra’s picture

Assigned: Unassigned » ganeshsurya11
Status: Active » Needs work
BeeOnLion’s picture

Hi all
I was wondering if I could volunteer some time to help resolve this issue as I think its a really handy module to have and if I can help resolve that would be great
Cheers

rajeshreeputra’s picture

@BeeOnLion patches are alway welcome, thanks!!!
Please proceed ahead!!

BeeOnLion’s picture

Hi @rajeshreeputra
Apologies I didnt come back to you on this is there anything I can help with you out with this patch. I have been looking at this module but I cant seem to find a fix for it yet

BeeOnLion’s picture

Hi @rajeshreeputra

I was just looking at the code but im not too sure if something like the below would work

I think you might be able to used this by triggering the storeLatLng function on the form's submit event. For example, if the form's id is my-form, you could add this line of code to trigger the function:

document.getElementById("my-form").addEventListener("submit", storeLatLng);

However im not 100% if it would work as havent tested it yet and would have to let you guys have a look at implementing it in the module

let lat, lng;
var user_location;
(function ($, Drupal, drupalSettings){
    'use strict';
    user_location = drupalSettings.user_location;
    Drupal.behaviors.appendVar = {
        attach: function(context, settings) {
            var myvar = 'this is the var i want to send';
            $('#edit-map-test').once('appendVar').val(user_location);
        }
    }
})(jQuery, Drupal, drupalSettings);

let map, infoWindow;
var previousMarker;
var defaultLatlng = { lat: 52.5404471, lng: -7.8272846 };

function storeLatLng() {
  // Get the value of the "webform_gmap_field" element
  const webform_gmap_field = document.getElementById("webform_gmap_field").value;
  // Parse the value as a JSON object
  const location = JSON.parse(webform_gmap_field);
  // Extract the latitude and longitude values from the JSON object
  lat = location.lat;
  lng = location.lng;
}

function initMap() {
    var user_location = drupalSettings.user_location;
    //console.log(drupalSettings.user_location+'***');
   // alert(user_location['lat']);
    //if(user_location) { defaultLatlng = JSON.parse(user_location); }
    map = new google.maps.Map(document.getElementById("map"), {
        zoom: 11,
        center: defaultLatlng,
    });
   
   // For edit wbform display (retrive the stored value and displayo on map)
   if(user_location) {
       previousMarker = new google.maps.Marker({
            position: JSON.parse(user_location),
            map: map
       });
       map.setCenter(JSON.parse(user_location));
       map.setZoom(14);
   } else {
document.getElementById("webform_gmap_field").value = JSON.stringify(defaultLatlng, null, 2);
       locateMe(map);
   }
    // Configure the click listener.
    map.addListener("click", (mapsMouseEvent) => {
        document.getElementById("webform_gmap_field").value = JSON.stringify(mapsMouseEvent.latLng.toJSON(), null, 2);
        if (previousMarker)
            previousMarker.setMap(null);
        if (current_marker_red)
            current_marker_red.setMap(null);
        previousMarker = new google.maps.Marker({
            position: mapsMouseEvent.latLng, 
            map: map
        });
    });
    
    infoWindow = new google.maps.InfoWindow();
    const locationButton = document.createElement("div");
    locationButton.className = "locate_btn";
    locationButton.textContent = "Locate me";
   
rajeshreeputra’s picture

@ganeshsurya, could you please look into it.

BeeOnLion’s picture

Hi @rajeshreeputra unfortunately I dont think I would be able to apply it as outside my knowledge base will have to leave it the dev team of the module sorry cant be of more help on it

vzsigmond made their first commit to this issue’s fork.

vzsigmond’s picture

Hey folks,

I've gone ahead and created an issue fork for this. Here's a merge request for you to check out: https://git.drupalcode.org/project/webform_gmap_field/-/merge_requests/3...

The main thing I've done is switch the element to a composite structure instead of a simple one. This change allows us to easily access the `lat` and `lng` properties.

To keep things smooth, I've added an update hook that handles the migration of existing submissions to the new composite structure.

Take a look and let me know what you think.
Cheers!

vzsigmond’s picture

Status: Needs work » Needs review
Andy_D’s picture

Big thanks to Tipperary County Council for sponsoring this enhancement!

Andy_D’s picture

Status: Needs review » Reviewed & tested by the community

Let's get this merged!

rajeshreeputra’s picture

rajeshreeputra’s picture

Version: 1.1.0 » 1.0.x-dev
Status: Reviewed & tested by the community » Fixed

Merged, Release will follow shortly!!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.