I'm using a proximity search in views for a store locator. The proximity search uses a text field for inputting the distance to search. I would like to change the input to a select list with predetermined distances. I've tried modifying the location_views_handler_filter_proximity.inc file. The select list will display, but it throws errors and no results. Any ideas and suggestions would be greatly appreciated.

I've been trying to change:

    $form['value']['search_distance'] = array(
      '#type' => 'textfield',
      '#title' => t('Distance'),
      '#default_value' => $this->value['search_distance'],
    );

To something like this:

    $distancemiles = array(10, 25, 50, 100);
    $form['value']['search_distance'] = array(
      '#type' => 'select',
      '#options' => $distancemiles,
      '#title' => t('Distance'),
      '#default_value' => $this->value['search_distance'],
    );

Comments

ryan88’s picture

I did the same thing. I think it is confusing to have an empty field for distance; a dropdown is easier. You are editing the right function, but not entering the values correctly. To be clear:

Edit /modules/location/handlers/location_views_handler_filter_proximity.inc
Go down around line 138.

Change :

    $form['value']['search_distance'] = array(
      '#type' => 'textfield',
      '#title' => t('Distance'),
      '#default_value' => $this->value['search_distance'],
    );

to

    $form['value']['search_distance'] = array(
      '#type' => 'select',
      '#title' => t('Distance'),
      '#options' => array(
         '' => t('Distance'),
        '10' => t('10 Miles'),
        '25' => t('25 Miles'),
        '50' => t('50 Miles'),
        '100' => t('100 Miles'),
        '250' => t('250 Miles'),
        '3000' => t('All'),
      ),
      '#default_value' => $this->value['search_distance'],
    );

Then edit the distances that you want.

hutch’s picture

Here is a patch of the above code. I took out the 'Miles' and added 1 and 5 for proximity searches in cities. On current cvs v 1.3.2.1

yesct’s picture

can someone make a patch to ADD a handler so people can pick which they want, to enter a distance, or use the drop down? What is involved in adding a handler?

hutch’s picture

I can see in the code how you might do that, eg provide both a textfield and a dropdown, add code to decide which one to apply etc but what I don't know is how to access the proximity filter. If someone sends me an example view, (one that takes an existing lid as an argument or however it works) I'll tackle it. At the moment I can't find the filter ;-(

ericprk’s picture

Thanks Hutch,

Worked like a charm. Heads up to anyone using this patch. This is probably due to editing an already existing view. The change threw an error until I reopened the exposed proximity filter and saved.

yesct’s picture

I opened #772302: how to add a handler and make it a choice for exposing a filter to see if views people can help point us at some docs.

merlinofchaos’s picture

If you're changing a textfield to a select list, you probably need to change the base class to views_handler_filter_in_operator() so that it gets the select handling stuff.

hutch’s picture

I have to admit that I'm flying blind here, but

Changing

class location_views_handler_filter_proximity extends views_handler_filter {

to

class location_views_handler_filter_proximity extends views_handler_filter_in_operator {

results in
PHP Fatal error: Class 'views_handler_filter_in_operator' not found in ....
in php.log along with WSOD

grepped for 'views_handler_filter_in_operator' and it *is* there

class views_handler_filter_in_operator extends views_handler_filter {
in views_handler_filter_in_operator.inc

views version 6.x-2.11

However the amount in the dropdown does get through to the query correctly, both with km and miles
Odd though, that class not being found ;-(

frankspin’s picture

subcribing

merlinofchaos’s picture

When changing the parent you also need to modify the declaration in hook_views_handlers() -- it's also likely to be slightly more complicated than this, because the in_operator class has a bunch of stuff to deal with the list of items. I recommend you look in the handlers directory and study that class and try to get a feel for what it's doing. It may mean that the child class looks significantly different.

hutch’s picture

OK, I'll have a look.

ksiebel’s picture

the best way to do that is via hook_form_alter

i did it like this:

function YOURMODULE_form_alter(&$form, $form_state, $form_id) {
	if($form_id == 'views_exposed_form') {
			if (isset($form['distance']['search_distance'])) {
			$form['distance']['search_distance'] = array(
			      '#type' => 'select',
			      '#title' => t('Distance'),
			      '#options' => array(
			         '' => t('Distance'),
			        '10' => t('10 Kilometer'),
			        '25' => t('25 Kilometer'),
			        '50' => t('50 Kilometer'),
			        '100' => t('100 Kilometer'),
			        '250' => t('250 Kilometer'),
			        '3000' => t('All'),
			      ),
			      '#weight' => 9,
			    );
			}

		  }
}

works like a charm for me...

tinem’s picture

Would it be possible to get links to sites where you got this to functioning, please?

Clint Eagar’s picture

StatusFileSize
new20.87 KB

Much thanks @ksiebel working great for me. Sorry @tinem I just have this working on my localhost. But I've attached a screen shot for your reference.

tinem’s picture

Maybe this discussion will interest some of you and especially Mortens tutorial http://groups.drupal.org/node/159584#comment-546544

I have made this http://mapadelic.tinemuller.dk/mapadelic_demo but would like to discuss this tutorial with other that try it because I have some questions. I don't use Features because I don't know how to use it.

ankur’s picture

Status: Active » Closed (fixed)

I've encountered a similar problem before (for a store finder, in fact) and have taken a rather hacky approach.

In my case, I used hook_form_alter() modify the exposed form for my view in the following way:

1. I added a select input to the form array, with a name different from the textfield provided
2. I then hid the default textfield by adding some inline CSS in my hook_form_alter()
3. Finally, I just added some javascript that populates the now hidden textfield for distance with whatever value has been chosen in my new select input.

Ideally, the drop-down would be an option when configuring the filter in the view. If there's an interest for that, I'd say create a new ticket with the feature request and set the version to 7.x-4.x, as that is where all new feature requests will be evaluated.

Clint Eagar’s picture

BTW this code works for D7 too!

function YOURTHEME_form_alter(&$form, $form_state, $form_id) {
	if($form_id == 'views_exposed_form') {
			if (isset($form['distance']['search_distance'])) {
			$form['distance']['search_distance'] = array(
			      '#type' => 'select',
			      '#title' => t('Distance'),
			      '#options' => array(
			         '' => t('Distance'),
			        '10' => t('10 Kilometer'),
			        '25' => t('25 Kilometer'),
			        '50' => t('50 Kilometer'),
			        '100' => t('100 Kilometer'),
			        '250' => t('250 Kilometer'),
			        '3000' => t('All'),
			      ),
			      '#weight' => 9,
			    );
			}

		  }
}
mrweiner’s picture

Where exactly did you place this in order to get it to work in D7? In the same spot as the original fix? Because when I do that, I don't get a dropdown menu, and instead just the miles/kilometers dropdown displays.

rickh’s picture

Hey clint, tried the code in a custom module and i'm getting the famous "An illegal choice has been detected. Please contact the site administrator." error. checked dblog and it says Illegal choice 5 in Distance element. Any ideas?

technikh’s picture

#17 worked for me. Thanks Clint Eagar

Fayna’s picture

Where do you put the code in #17? In your theme's template.php file? That didn't seem to work for me in Drupal 7.14.

mhurston’s picture

Thank you! ryan88 's post worked fo rme, though I found it on line 206 of the version I had.

sakadava’s picture

Issue summary: View changes

If you get "An illegal choice has been detected. Please contact the site administrator." and in the log you're seeing Illegal choice x in Distance element - this is possibly because you've a default value set for distance in the views exposed filter which conflicts with the values you've coded in the hook_form_alter(). Make sure the two correspond to each other.