Custom Fields
Re-Envisioned
Support Meta Box Geolocation Only auto populate neighborhood field if not empty

This topic contains 4 replies, has 2 voices, and was last updated by  pluginoven 3 days, 19 hours ago.

  • Creator
    Topic
  • #13165
    Resolved
    pluginoven
    Participant

    I have a field for the neighborhood. Sometimes google will return a neighborhood value for an address and this will autopopulate because the field ID auto binds:

    array(
        'type' => 'text',
        'name' => __('Neighborhood', 'psych-info'),
        'id'    => 'neighborhood',
    ),
    

    The issue is when google does not return a neighborhood value, then this field is cleared of any existing value. I know I can change the ID to prevent the auto-binding, but maybe this could be improved?

    I can think of a couple of workarounds:

    1. add a fill bind_if_empty flag in the plugin:
      array(
          'type' => 'text',
          'name' => __('Neighborhood', 'psych-info'),
          'id'    => 'neighborhood',
          'binding' => 'neighborhood',
          'bind_if_empty' => false,  //default true
      ),
      

      bind_if_empty might also accept a secondary field to bind if the first field is empty:

      'binding' => 'neighborhood',
      'bind_if_empty' => 'sublocality'
      
    2. Allow the binding to be filtered by a function

    I am happy to extend the plugin. If so, is there a private repo I can submit a pull request to?

    P.S. There is a typo in the Features section on the extension’s page:

    Auto popular all location fields when user enter an address

    should read:
    Auto populate all location fields…

Viewing 4 replies - 1 through 4 (of 4 total)
  • Author
    Replies
  • #13167

    pluginoven
    Participant

    Update: I have added a working bind_if_empty. Please me know how best to submit a pull request.

    #13277

    pluginoven
    Participant

    Two weeks and crickets… The following changes would need to be made to include the
    'bind_if_empty' => false, //default true
    idea described above.

    File: js/mp-geo.js
    Line: 85

    var $this = $( this ),
        dataBinding = $this.data( 'binding' ),
        dataBindEmpty = $this.data( 'bind_if_empty' ),
        addressField = $this.data( 'address_field' ),
        fieldValue = that.getFieldData( dataBinding, place );

    File: inc/class-meta-box-geolocation.php
    Line: 97

    $binding = isset( $field['binding'] ) ? $field['binding'] : $this->guessBindingField( $field['id'] );
    $bind_if_empty = isset( $field['bind_if_empty'] ) ? $field['bind_if_empty'] : 1;
    $address_field = isset( $field['address_field'] ) ? $field['address_field'] : '';
    
    if ( $binding ) {
                $begin .= '<script type="html/template" class="rwmb-geo-binding" data-binding="' . esc_attr( $binding )
                . '" data-bind_if_empty="' . esc_attr( $bind_if_empty )
                . '" data-address_field="' . esc_attr( $address_field )
    ...

    Would be fantastically 2019 if there was a magical way to submit a pull request.

    #13293

    Anh Tran
    Keymaster

    Thanks for your help!

    I’ve sent an invitation to you on Bitbucket, so you can have access to the repo and open a PR!

    Cheers!

    #13310

    pluginoven
    Participant

    Thank you. PR has been submitted.
    Issue may be marked as resolved.

Viewing 4 replies - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.