Description
Allows the mapped autocomplete values to be customized before the values are added to the inputs.
Usage
gform.addFilter( 'gform_geolocation_autocomplete_mappings_pre_populate', function ( mappings, components, place, formId, fieldId ) {
return mappings;
} );
Parameters
- mappings object
A collection of address field elements and values to be populated. Only the values are writable.
The keys to the object are:line1
,line2
,city
,state
,zip
,country
,latitude
, andlongitude
. Each key is assigned an object that contains the following:- element object
A JavaScript element containing the input or select to be populated. - value string
The value to be populated into the input or select.
- element object
- components object
The components for the selected place. Details about how the component objects are defined can be found here. Each component can be accessed using the type as the key, e.g.components.street_number.long_name
. - place object
Data about the place selected by the user. Details about what the place object can contain are here. - formId number
The ID of the form the address field belongs to. - fieldId number
The ID of the address field being populated.
Examples
Change line 1 value
The following shows how the entire address of the selected place can be populated into the line 1 input.
gform.addFilter( 'gform_geolocation_autocomplete_mappings_pre_populate', function ( mappings, components, place, formId, fieldId ) {
const tempElement = document.createElement( 'div' );
tempElement.innerHTML = place.adr_address;
mappings.line1.value = tempElement.innerText || tempElement.textContent;
tempElement.remove();
return mappings;
} );
Change the line 2 value
The following shows how you can add a custom value to the line 2 input
gform.addFilter( 'gform_geolocation_autocomplete_mappings_pre_populate', function ( mappings, components, place, formId, fieldId ) {
mappings.line2.value = 'myVal';
return mappings;
} );
Placement
Your code snippet can be placed in an HTML field on your form or in a theme custom JavaScript file.
See also the JavaScript/jQuery section in this article: Where Do I Put This Code?
Source Code
This filter is located in getMappings()
in google-places.js.
Since
Geolocation 1.3.0