Hi,
I'm building a view of homes/properties with exposed filters of "Minimum Rent" and "Maximum Rent" - so that people can find homes according to their budget.

Rent (per month) is basically an integer field and can be whichever number (£475 or £677 or whatever an end-user enters when creating the node). Right now in my automatically-refreshing exposed filters block I have "Min Rent" and "Max Rent" as textfields (Views won't allow anything else). The trouble is that if you are not typing "400" fast enough the view often refreshes with "4" or "40" = BAD user experience.
But in my exposed filters for "Min Rent" and "Max Rent" I want it to be a dropdown list of 100, 200, 300, 400, 500 etc -in other words, automatically fixed, so that it is quicker and user-friendlier to determine the min and max range. Exactly as we see on websites like www.rightmove.co.uk or www.zoopla.com or any other property listing website.).

It seems like it could be pretty easily achieved. I have come across a tutorial that does something similar here http://zugec.com/drupal/creating-custom-filters-in-views-2 and in this thread http://groups.drupal.org/node/25941 people are sugesting that it is possible to use hook_form_alter to achieve this.

I don't think I can achieve this with any other module and Better Exposed Filters seems to be the closest. If it works, it can have many-many other useful applications for filters that establish range like "minimum bedrooms" and "maximum bedrooms". "Min Price" and "Max Price" etc

The more I searched for a solution, the more I noticed that many people need this in one variant or another. So I thought why not turn to Better Exposed Filters module and ask them to make it available for Drupal community.

Feature request:
in Better Exposed Filters views-settings please give us a textarea where we can enter the values on separate lines (100, 200, 300, 400, 450, 500, 550 OR even texts like "Dogs", "Cats", "Foxes" or whatever) and the module will turn those textfield filters into a dropdown list filter with those values showing.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

drupalina’s picture

Here I'm attaching a small module called "Selectable Price" that I wrote to achieve this functionality for my "rentpcm" field. Works very well in D7 (but for some reason it also ads "No min" and "No max" boxes on all forms throughout the site including node creation forms and admin configuration forms). Maybe you could take it and incorporate its logic into your module for filters which establish the "in between" min and max range values.

<?php

function selectable_price_form_alter(&$form, $form_state, $form_id) {

  $field_rentpcm = 'field_rentpcm';
  
  if (empty($form_state['view']->exposed_input[$field_rentpcm])) {
    $form_state['input'][$field_rentpcm] = array(); 
  }
  unset ($form[$field_rentpcm.'_value']['min']);
  unset ($form[$field_rentpcm.'_value']['max']);

    $price_min_options = array(
         '0'     => t('No Min'),
         '100'   => '100 pcm',
          '200'   => '200 pcm',
	  '300'   => '300 pcm',
	  '400'   => '400 pcm',	  
          '500'   => '500 pcm',
          '600'   => '600 pcm',
	  '700'   => '700 pcm',
	  '800'   => '800 pcm',
	  '900'   => '900 pcm',
	  '1000'   => '1,000 pcm',
	  '1250'   => '1,250 pcm',
          '1500'   => '1,500 pcm',
	  '1750'   => '1,750 pcm',
	  '2000'   => '2,000 pcm',
          '2500'   => '2,500 pcm',
	  '3000'   => '3,000 pcm',
          '3500'   => '3,500 pcm',
	  '4000'   => '4,000 pcm',
	  '5000'   => '5,000 pcm',
	  '6000'   => '6,000 pcm',
	  '7000'   => '7,000 pcm',
	  '8000'   => '8,000 pcm',
	  '9000'   => '9,000 pcm',
	  '10000'  => '10,000 pcm',
    );
    $price_max_options = array(
          '100'   => '100 pcm',
          '200'   => '200 pcm',
	  '300'   => '300 pcm',
	  '400'   => '400 pcm',	  
          '500'   => '500 pcm',
          '600'   => '600 pcm',
	  '700'   => '700 pcm',
	  '800'   => '800 pcm',
	  '900'   => '900 pcm',
	  '1000'   => '1,000 pcm',
	  '1250'   => '1,250 pcm',
          '1500'   => '1,500 pcm',
	  '1750'   => '1,750 pcm',
	  '2000'   => '2,000 pcm',
          '2500'   => '2,500 pcm',
	  '3000'   => '3,000 pcm',
          '3500'   => '3,500 pcm',
	  '4000'   => '4,000 pcm',
	  '5000'   => '5,000 pcm',
	  '6000'   => '6,000 pcm',
	  '7000'   => '7,000 pcm',
	  '8000'   => '8,000 pcm',
	  '9000'   => '9,000 pcm',
	  '10000'  => 'No Max',
    );
  $form[$field_rentpcm.'_value']['min'] = array (
    '#type'           => 'select',
    '#multiple'       => false,
    '#required'       => false,
    '#options'        => $price_min_options,
    '#default_value'  => 'No Min',
  );      
  $form[$field_rentpcm.'_value']['max'] = array (
    '#type'           => 'select',
    '#multiple'       => false,
    '#required'       => false,
    '#options'        => $price_max_options,
    '#default_value'  => 'No max',
  );  

}

So, what would be a cool feature for Better Exposed Filters (BEF) module for "in between" filters (where min and max range is established) is to have one textfield where admins could enter for example:
'0' => t('No Min'),
'100' => '100 pcm',
'200' => '200 pcm',
and another textfield where they could enter
'100' => '100 pcm',
'200' => '200 pcm',
'300' => '300 pcm',
'10000' => t('No Max'),
and then the BEF module would incorporate it into its code and create the effect like the above.

What do you think of the idea?

a.ross’s picture

+1, need to achieve the same thing. Maybe I'll have a go at patching later.

a.ross’s picture

Status: Active » Needs review
FileSize
3.67 KB

Alright, patch is done!
It's not ideal, in that it doesn't allow key|value pairs for dropdowns and you have to set a default option in a separate dialog (views' own filter configuration dialog). It works fine though, so please review!

Status: Needs review » Needs work

The last submitted patch, dropdown_selection_filter_for_numeric_fields-1556670-3.patch, failed testing.

a.ross’s picture

Patch failure seems to have something to do with line endings. We use windows at the office.

Here's a new patch, which actually supports key|label pairs, by utilizing the List module from core.

a.ross’s picture

Status: Needs work » Needs review
FileSize
4.05 KB

Change to Needs review

Status: Needs review » Needs work

The last submitted patch, dropdown_selection_filter_for_numeric_fields-1556670-5.patch, failed testing.

junkbox’s picture

Drupalina,

Your module does the job nicely, the reason you're getting the min/max on all forms is because you're doing a generic form_alter (meaning all forms) what you're looking for is:

selectable_price_form_views_exposed_form_alter <--- at least for this application

Other than that, saved me a lot of time thanks!

mazdakaps’s picture

Is there any update on this? Also drupalinas module how can i make it work? I created a module with drupalinas code i enable it at modules section but after that i dont know what to do.
Thank you very much

a.ross’s picture

Status: Needs work » Needs review
FileSize
4.04 KB

Re-rolled my old patch. Hope this one at least applies.

Status: Needs review » Needs work

The last submitted patch, dropdown_selection_filter_for_numeric_fields-1556670-10.patch, failed testing.

a.ross’s picture

Ugh. DOS line endings yay...

a.ross’s picture

Status: Needs work » Needs review
mazdakaps’s picture

Hello i just run the patch . If i didnt do something wrong i selected for the price filter the drop down with preset values. The problem is that i dont know how to insert the preseted values so the drop down menus are empty.
Thank you

a.ross’s picture

It works just like a regular list field. Here's an example of input:

10|$10,-
50|$50,-
100|$100,-

You should also set the default value of the dropdown in the Views' filter, otherwise you'll get an error.

mazdakaps’s picture

I am sorry but i didnt understand
i created a filter the field_price and i use is between i insert one min and one max value but i dont understand where to insert the values for the list field
10|$10,-
50|$50,-
100|$100,-

Sorry again but my brain stucked

a.ross’s picture

FileSize
139.19 KB

It's under 'More options for "Price"'

mazdakaps’s picture

Hmm thats why i cant insert the values. I selected the drop down but these options i see in the photo are missing. Ill try to patch it again and ill inform you.
Cheers

mazdakaps’s picture

ok i repatched it, followed your instructions and it seems that is working. I will test it and ill inform for any issues.
Thank you very much

mazdakaps’s picture

One thing in dont know if it is a bug if i punt the values and i want to change them again when i go to change them the values arent there anymore

a.ross’s picture

Did you input the default value in the normal Views filter dialog?

mazdakaps’s picture

yes in the normal filter dialog i insert the min and max values because if i dont it gives me an error. It isnt that is not working but if i want to make changes of the values of the dropdown even if it works the values arent there anymore and i have to put them again.

a.ross’s picture

It works for me. Can you paste your options?

mazdakaps’s picture

FileSize
77.23 KB

Ok in the picture i you can see the option boxes without the options inside even if it works after i insert the option when i go there to make a change there isnt anything there for some reason.
The options i user is something like yours in your picture

0| Min
1000
2000
3000
4000
5000

and

99999999| Max
1000
2000
3000
4000
5000

a.ross’s picture

Ahh, alright. That's quite strange, and I can't seem to reproduce it... :/

Can you find the following line in: better_exposed_filters_exposed_form_plugin.inc
$bef_options[$label]['more_options']['bef_numeric_list_0'] = array(
and put the following before it:
dsm($existing[$label]);

Install the devel module and see what output you get when you open the BEF dialog.

estevegri’s picture

FileSize
66.5 KB

Hello. This is the fisrt time that I write here, so I'm sorry if I make any error in this message.

I'm using Drupal 7, Views 3 and BEF.

I have an exposed filter in views that have an integer field where I insert prices. That filter is "in betwen" mode. I would like to make this exposed filter as a dropdown.

First question: I copied the code of the patch #12 at "better_exposed_filters_exposed_form_plugin.inc", but I'm not sure where I have to insert the third part of that code. I mean the last part of code that starts with:

+ case 'bef_dropdown':
+ if (!module_exists('list')) {
+ break;
+ }
+ $formfield = array(
...

In any case, I can choose dropdown in "BEF settings / exposed filter settings" for the price field, but I can't see the textareas where I have to insert the two list of prices for min and max prices. I have attached a screen of "BEF settings". If I save these settings, then I see the filter with dropdowns but without any value to choose. Additionally, If I return to the "BEF settings / exposed filter settings", the price field settings is configured to "Default numeric field" again.

I would appreciate any help.

Thanks a lot.

a.ross’s picture

Read this to learn how to properly apply patches: http://drupal.org/patch/apply

estevegri’s picture

Status: Needs review » Active

a.ross, thanks and sorry for my basic question.

I applied correctly the patch and seems that it is working fine. As said in #13 I set the default min and max values of the dropdown in the Views filter (thanks again a.ross).

But now, I get an issue with the conditional fields module. I would like to make the min and max exposed filter dependent to another filter. I created the dependencie like always but it is not applied. The min and max exposed filter is always showing. Any ideas why?

Thanks a lot

a.ross’s picture

Hm, as far as I understand, Views support in Conditional Fields is still under development: http://drupal.org/node/961406

Regardless, that could be a pretty hard-to-fix issue.

dgastudio’s picture

can this patch be migrated to latest stabe version of BEF?

u5444@krypton ~/domains/auto.u5444.krypton.vps-private.net/sites/all/modules/bet ter_exposed_filters $ curl http://drupal.org/files/dropdown_selection_filter_for
_numeric_fields-1556670-12.patch | git apply
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
101 4148 101 4148 0 0 8506 0 --:--:-- --:--:-- --:--:-- 13168
error: patch failed: better_exposed_filters_exposed_form_plugin.inc:296
error: better_exposed_filters_exposed_form_plugin.inc: patch does not apply

dgastudio’s picture

ok, applied the patch to beta 3 version, works perfectly except the same problem as in #24.

up. it's becouse $existing is empty.

a.ross’s picture

As I expected. I wouldn't know why $existing is empty though. Maybe the variable name was changed? In any case, I don't think I will be able to support this patch any longer. I hope someone else takes it and improves upon it. I'd like to see this stuff committed.

dgastudio’s picture

i have found the problem

simply replace $existing with $this->options['bef'].

mikeker’s picture

Assigned: Unassigned » mikeker
Status: Active » Needs review

Apologies for letting this sit for so long. I'll try and review it in the next week.

a.ross’s picture

That would be great! :)

mikeker’s picture

Status: Needs review » Postponed

@a.ross, I appreciate the time and effort you've put into this issue, but I'm going to postpone as #731662: Hybrid Exposed Filters has been in the Views -dev releases for for a couple months now and will offer more flexibility once they add greater/less than options. (I didn't know about it until I had to debug a Views' bug and pulled the latest code from Git or I would've mentioned this earlier.)

Granted there are a lot of bugs with the current implementation of Grouped Filter (aka Hybrid Filters) at the moment, but I think it'll be a better solution eventually.

Give Grouped Filters a try and feel free to reopen this issue if you feel otherwise.

a.ross’s picture

Whoa! That's an interesting issue. Looks like it's already in the stable release as well! Good find.

dgastudio’s picture

maybee you are right, but remember that with new hybrid filter you can only populate value "equal to", not range.

i mean, with this patch, i can prepopulate values for "BETWEEN" value.

and really need it.

sorry for my english.

a.ross’s picture

Ah, I see what you mean, as the end user, you get a dropdown for both the "from" value and the "to" value, a different solution to the hybrid filters in Views, which you can use to make a preset, such as: Between 10 and 100, between 100 and 1000.

dgastudio’s picture

yes, you are right.

i have applied manually the the patch and it works perfectly.

the only thing that will be really cool, is if we can enter KEY / VALUE in lists instead of only KEY

i mean.

now for example, to set price range i have to :

100
200
300

with key value feature, it will be

100|100 $
200|200 $
300|300 $

a.ross’s picture

This should already be possible in fact :)
It does require enabling the List module from core iirc. Maybe just the Options module.

ensignavenger’s picture

I have applied this patch it it appears to work quite well! I looked at the solution in #36, but I did not want to use the dev version of views on my site, and it appears to be not quite ready, whereas this patch seems to work much better.

One question, in several comments, setting the default value is mentioned- but how exactly do you do that? I suppose this is more of a views issue, as it has to be done in the regular views filter dialog- but I can't seem to find where or how, and my problem applies directly to getting the patch to work properly, so I thought I'd mention it here!

ensignavenger’s picture

Never ceases to amaze me- I search and search for a solution, and then I post asking others- and then I find the answer!

My problem was I had put a value into the filter box for the default that was not used as an option for the select box- and thus it could not be the default...

Thanks for the great patch!

johnhorning’s picture

I was about to ask what the status of this is, but then discovered that this feature is now built into views and called grouped filters.

Neslee Canil Pinto’s picture

Status: Postponed » Closed (won't fix)

Hi, there will be no more future development for 7.x branch. If you see this issue in 8.x, feel free to file an issue. Closing this as Closed(wont fix).