2

Using Geofield with Google's API, when a user enters "Vancouver", it assumes I mean "Vancouver, Washington", when I actually want "Vancouver, BC". Is there any way to append "Canada" to the location info submitted to google?

It would be nice if I could do this with some override but will accept a hack to the module and I will just keep track of the modified module.

Here is a link to Geofeild's code that covers this. http://pastebin.com/Diwj5deE

Note: That I just need the country appended to the info sent to google. I don't want the filter to have a default of "Canada" and it can't prefilter the view with "Canada". It is not until the user actually uses the exposed filter.

2
+50

As with any form, a custom module can modify the #submit array to put a submission handler in front of the views exposed filter form to preprocess the data.

Here we're just modifying the submitted origin value so that " Canada" is appended if any text is entered by the user:

/**
 * Implements hook_form_views_exposed_form_alter().
 */
function MYMODULE_form_views_exposed_form_alter(&$form, &$form_state, $form_id) {
  array_unshift($form['#submit'], 'MYMODULE_form_views_exposed_form_submit');
}

/**
 * Modify submission value if given origin has been supplied.
 */
function MYMODULE_form_views_exposed_form_submit($form, &$form_state) {
  if (isset($form_state['values']['field_geofield_distance']['origin']) && 
      strlen($form_state['values']['field_geofield_distance']['origin']) > 0) {
    $form_state['values']['field_geofield_distance']['origin'] .= ' Canada';
  }
}
  • Quick follow up question. Any reason whay I might be seeing this php notice in my logs? It appears to be due to this code. Notice: Undefined index: field_geofield_distance in custom_form_views_exposed_form_submit() – Nigel Waters Sep 20 '16 at 22:52
  • The example code didn't cover the scenario where field_geofield_distance might not be defined. You can add a isset() call to the conditional to catch the warning. – Shawn Conn Sep 20 '16 at 23:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.