From 6446042527d5c7f5e82cd1296d62c1eba169841f Mon Sep 17 00:00:00 2001 From: saifsultanc Date: Mon, 29 Apr 2024 17:02:18 +0530 Subject: [PATCH] `gpaa-single-line-as-input.php`: Added full address capability to the Single Line as Input functionality. --- .../gpaa-single-line-as-input.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/gp-address-autocomplete/gpaa-single-line-as-input.php b/gp-address-autocomplete/gpaa-single-line-as-input.php index e73450acf..71bb0114f 100644 --- a/gp-address-autocomplete/gpaa-single-line-as-input.php +++ b/gp-address-autocomplete/gpaa-single-line-as-input.php @@ -7,7 +7,7 @@ * * Use a Single Line Text field as autocomplete input and populate the Single Line Text field with the full address. * - * Note: This snippet will require the user to make a selection from the auto-suggested addresses. + * Note: This snippet will require the user to make a selection from the auto-suggested addresses. * * Plugin Name: GP Address Autocomplete - Use Single Line Text field as Autocomplete Input * Plugin URI: https://gravitywiz.com/documentation/gravity-forms-address-autocomplete/ @@ -60,7 +60,14 @@ public function output_script() { gform.addFilter('gpaa_values', function (values, place, gpaa, formId, fieldId) { if ( args.formId == formId && args.addressFieldId == fieldId ) { - values.autocomplete = place.formatted_address; + if ( args.useFullAddress ) { + // Logic borrowed from https://github.com/gravitywiz/snippet-library/pull/730 + var fullAddress = gpaa.inputs.autocomplete.value; + values.autocomplete = fullAddress; + values.address1 = fullAddress.split(',')[0].trim(); + } else { + values.autocomplete = place.formatted_address; + } $input.data('gpaa-filled-value', place.formatted_address); } @@ -94,6 +101,7 @@ public function add_init_script( $form ) { 'formId' => $this->_args['form_id'], 'addressFieldId' => $this->_args['address_field_id'], 'singleLineFieldId' => $this->_args['single_line_field_id'], + 'useFullAddress' => $this->_args['use_full_address'], ); $script = 'new GPAASingleLineInput( ' . json_encode( $args ) . ' );'; @@ -125,4 +133,5 @@ public function is_applicable_form( $form ) { 'form_id' => 123, // The ID of your form. 'address_field_id' => 4, // The ID of the Address field. 'single_line_field_id' => 5, // The ID of the Single Line Text field. + // 'use_full_address' => true, // Uncomment to use the full street address if you don't want an abbreviated street address. ) );