Location search trick with Views exposed form elements

Posted on July 13, 2010 at 12:00 pm

We are just about to add have recently added location-based services to the BeGrand.net website and will be offering a way to search for events close to your post code.

As part of the build we’re using the excellent Location module which provides a proximity search by default. The latest version at time of writing is 6.x-3.1.

We have an exposed block that provides the basic search form and this leads to a Google map displaying events nearby – so you can search for events within 10 miles of CB24 for example. The search works well, but I wanted a way of displaying the filter on the output page. I knew that I could specify PHP code in the header for the view on the page in Views, but struggled to find the code I needed to return the post code, distance and units to show. Finally, I found the answer. Here it is:

<?php
  $view = views_get_current_view();
  if (isset( $view->exposed_input['distance'])) {
    echo "<h2>Upcoming events within ";
    echo $view->exposed_input['distance']['search_distance']." ";
    echo $view->exposed_input['distance']['search_units']."s of ";
    echo $view->exposed_input['distance']['postal_code']."</h2>
  ";
  } else {
    echo  "<h2>All upcoming events</h2>";
  }
?>

So now, when you search within 10 miles of CB24 the header is displayed as:

Upcoming events within 10 miles of CB24

I’ve added a conditional statement so that an alternative header is displayed if the filter hasn’t been set.

Job done, which is nice.

Tags: , ,

Leave a Reply