Improving the UI of Drupal Proximity Searches
In this Drupal video tutorial, we improve the UI of our proximity search (created with the Views, Gmap and Location modules) by validating postal codes submitted by users and displaying a status message if there are no nodes within the given proximity.
To achieve this, this tutorial shows you how to implement hook_form_alter to return a message to the user if they incorrectly enter a postal code and hook_views_pre_render to check our views result and display a message if no results are being returned.
This tutorial is predicated on the following video tutorials:
Installing and Configuring the Gmap and Location Modules
Drupal Location-Gmap Proximity Searches
Drupal Module Development Tutorials (optional tutorials to review)

Comments
Works great and my client is
Works great and my client is most satisfied with the end result of there new Store Locator.
Thanks Pete!
Out of curiosity, why are the
Out of curiosity, why are the files available for download not what they seem? The code from the tutorial isnt in the module file.
Hey danny, I'll have to
Hey danny,
I'll have to check that out... I had a couple different versions for different projects I was working on so it's possible i just uploaded the wrong one.
Thanks for pointing it out and glad the tutorial helped.
pete
Thanks Pete. I'll checkback
Thanks Pete. I'll checkback regularly to see when you have updated those files so I can compare with my own.
PS: Are you available for contract work? I'm always looking for good programmers on various projects.
Thanks,
Danny
Hey Danny, were you able to
Hey Danny,
were you able to download the files? I kept getting a 403 error, permission denied - it's a server setting and rather than work with my host to fix it now (htaccess hack didn't work), I just made a rar file. Everything should be good to go. I also sent you an email regarding the work question.
Pete
You know what would be
You know what would be awesome? If the script added the space automatically for you after typing the first 3 characters. Not sure if this is possible.
Hey Danny, you'd need some
Hey Danny,
you'd need some javascript to do that... my inclination would be just to check the postal code and if it's six digits, rewrite the value with the space in it...
Actually, it's pretty easy to
Actually, it's pretty easy to do through the validation function:
function dao_m_locator_validate($form, &$form_state){
$pc = ($form['#post']['distance']['postal_code']);
if($pc != '') {
if (strlen($pc) < 6){
form_set_error('postal_code', t('You did not enter enough digits in your postal code.'));
}
elseif (strlen($pc) == 6 && strpos($pc, " ") == false) {
$form_state['values']['distance']['postal_code'] = substr($form['#post']['distance']['postal_code'], 0, 3) . " " . substr($form['#post']['distance']['postal_code'], 3);
}
elseif (strlen($pc) > 7){
form_set_error('postal_code', t('You entered too many digits in your postal code.'));
}
}
}
sorry for my ignorance but
sorry for my ignorance but where can I write this code ?
I don't mind writing directly in source at this point
many thanks in advance
For a validation function
For a validation function like this, you would do it in a custom module.
With regards to adding the space automatically - I agree, a validation function is easiest but with regards to my comment about javascript, i meant if you wanted to add the space as the person was actually typing (which is what I think the commenter meant), you would need javascript.
pete
Hey Peter, thanks for your
Hey Peter, thanks for your tutorials, its always easier learning on someone else's mistakes... :) .
Trying to make this module work for Drupal 7 and get an error
Notice: Undefined index: #post in locator_alter_validate() (line 21 of /home/content/16/5470416/html/advisorplace/sites/all/modules/locator_alter/locator_alter.module).
Hi Nathan, glad the tutorials
Hi Nathan, glad the tutorials are helping - can you provide some details about your set up? i.e., version of drupal, views, location, etc. Also, did you check to see what line 21 was doing in the location module?
pete
I followed your other
I followed your other tutorial for Drupal 7.
Drupal: 7.7
Views: 7.x-3.0-rc1
Location: 7.x-3.x-dev
Line 21:
$pc = ($form['#post']['distance']['postal_code']);I suspect the reason is that in Drupal 7 there is a new function views_form_validate() . Maybe this is the one i have to use instead?
Also when i do a DSM printout i don't see [#post] anywhere?
Thanks,
Nathan
Nathan, I too had an issue
Nathan,
I too had an issue with this (just started using Drupal about 6 months ago)... came up with a possible fix...
[code]
$pc = ($form['distance']['postal_code']['#value']);
[/code]
This worked for me: $pc =
This worked for me:
$pc = ($form['distance']['postal_code']["#value"]);
Wow, this was racking my
Wow, this was racking my brain for ages! I must have looked through this list ten times and didn't notice the #value field.
Thanks very much guys and the tutorials are awesome.
Hey Peter, so the tutorial
Hey Peter, so the tutorial was great so far however ran into a couple of things such as having to rewrite the alter_validate postal code variable reference to:
$pc = ($form['distance']['postal_code']["#value"]);
But my big problem right now which I can't seem to resolve is being able to access $view in the view_pre_render function.
When I try to do:
function locator_alter_view_pre_render(&$view) {
dsm($view);
}
Nothing is returned.
I'm not sure if it's because perhaps drupal 6 to 7 has changed or if it's because my map and form is actually in a block and attachment on the main page of my site vs. the page generated when first creating a view.
Any help would be appreciated. Thanks.
Orenbus, Looks like the API
Orenbus,
Looks like the API has changed in Views 3 for the hook_views_pre_render
On Drupal.org there is no documentation for Views 3 yet and had a quick look through the forums but have come up blank. There are a few people having issues upgrading to Views 3 too.
I will keep hunting.
Hi Orenbus / Barry, sorry for
Hi Orenbus / Barry,
sorry for my late response but I think the Views 3 hook remained the same -- hook_views_pre_render(&$view). I noticed in your comment you had location_alter_view_pre_render -- no "s" on views.
I actually haven't run this module on drupal 7 / views 3 yet but according to views/docs/views.api.php the look remained the same and is called right before the render process. It's at line 641 of that file.
Let me know if that helps.
Pete
Post new comment