Views exposed filters for CCK field with allowed values list.

grantkruger's picture

I have a CCK content type with a field that has an allowed values list. This of course works great and when creating the content I get a select list showing only the values I specified. However, Views ignores this list when it comes to the use of an exposed filter on this field. Views creates an exposed filer that is merely a text box, without even an autocompleter. Any suggestions on how I can override this filter with a select box that has the same values as my CCK allowed values list? Even an answer where I have to specify the list again would do me for now.

Thanks for your time.

Comments

hook_form_alter()

duellj's picture

You can use hook_form_alter to change the exposed filters form, like so:

<?php
function mymodule_form_views_exposed_form_alter(&$form, $form_state) {
  if (
$form['#id'] == 'views-exposed-form-view-id-display-id') {
   
$form['exposed-element']['#type'] = 'select';
   
$form['exposed-element']['#options'] = array(....);
  }
}
?>

You can either manually define the options for the select box, or try and figure out if there's an easy function call to cck to get the allowed values list.

Thanks. That's interesting.

grantkruger's picture

Thanks. That's interesting. Unless I missed something, the implication is I need to create a custom module in order to alter the form. Is it common practice to set up a form-altering module for all such hooks? Or am I (as seems likely) missing something?

Sala kahle,
Grant

Obviously eliza411's

duellj's picture

Obviously eliza411's solution is better :), but to answer your question: yes, you would put this in a custom module. Usually with larger projects I create a helper module that holds all my form overrides, custom blocks, views definitions, and update hooks (for making configuration changes; see drewish's post on the subject: http://drewish.com/node/123).

The way I've used form alter and views exposed form in the past is to create a range select list out of a numeric text field (e.g. "$100-$200", etc., for a price float field). Not sure if there was a better way to do it, but it worked, and I guess in a crunch that's what matters.

Thanks. That's helpful for

grantkruger's picture

Thanks. That's helpful for the final phase of our project, which is upcoming after my trip home to South Africa.

Sala kahle,
Grant

@duellj: I am looking for

aschiwi's picture

@duellj: I am looking for pretty much exactly this use case. I have two range text fields which I need to turn into select lists. Do you happen to have a working snippet for a custom module? Since I don't know much php I can't work out your example code in #1 to do what I want. I guess I only need to know what goes in $form['exposed-element']['#options'] = array(....); or maybe that line could be left out?

To create a single select

duellj's picture

To create a single select field from a views filter setup as a range is actually a two part process. First, you define your hook_form_alter(). If you have a custom module called "mymodule":

<?php
function mymodule_form_views_exposed_form_alter(&$form, $form_state) {
  if (
$form['#id'] == 'views-exposed-form-view-id-display-id') {
   
$form['exposed-element']['#type'] = 'select';
   
$form['exposed-element']['#options'] = array(
     
'100-200' => '$100-$200',
     
'200-300' => '$200-$300',
     
'300-400' => '$300-$400',
     
'400-500' => '$400-$500',
     
'500-600' => '$500-$600',
     
'600-700' => '$600-$700',
     
'700-800' => '$700-$800',
     
'800-900' => '$800-$900',
     
'900-1000' => '$900-$1000',
    );
   
$form['#submit'][] = 'mymodule_form_views_exposed_form_alter_submit';
  }
}
?>

In this function, you need to replace a couple of strings:

  • "view-id" = The id of the view, called the "View name" when you setup the view.
  • "display-id" = The id of the display you want to alter. If you hover over the link in the left tabs when editing the view, it'll be what's after the "#views-tab-", probably something like page_1.
  • "exposed-element" = The "Filter identifier" for the filter you want to change.
  • You can also edit the options array to be any combination of prices you want.

You also need a submit function to split the ranges back up so views knows what to do with them:

<?php
function mymodule_form_views_exposed_form_alter_submit(&$form, &$form) {
  if (
$form_state['values']['exposed-element']) {
    list(
$values['min'], $values['max']) = explode("-", $form_state['values']['exposed-element']);
   
$form_state['view']->exposed_data['exposed-element'] = $values;
  }
}
?>

Let me know if that makes sense.

Need CCK field in select option using Views

Anitha Rajesh's picture

@duellj : Nice explanation provided for exposed filter. Need to search the fields based on content type fields using views. I.e it's a library site, have provided the fields like author, publications etc in the cck(as text field). Now using views module, need to search based on these items. It's like by providing the keyword, need to chose these CCK fields(author,publications..) in the select list.

Can able to list as autocomplete field in views filter by using exposed filter option, but is it possible to have the CCK text fields in the options array?

Thanks,
Anitha

Hey duellj, Thanks for taking

aschiwi's picture

Hey duellj,

Thanks for taking the time to help me out. I created a module containing the following code:

<?php
function agerange_form_views_exposed_form_alter(&$form, $form_state) {
  if (
$form['#id'] == 'views-exposed-form-bday-page-2') {
   
$form['field_alter']['#type'] = 'select';
   
$form['field_alter']['#options'] = array(
     
'18-24' => '18-24',
     
'25-29' => '25-29',
     
'30-35' => '30-35',
    );
   
$form['#submit'][] = 'agerange_form_views_exposed_form_alter_submit';
  }
}


function
agerange_form_views_exposed_form_alter_submit(&$form, &$form) {
  if (
$form_state['values']['field_alter']) {
    list(
$values['min'], $values['max']) = explode("-", $form_state['values']['field_alter']);
   
$form_state['view']->exposed_data['field_alter'] = $values;
  }
}
?>

This ADDS a dropdown, but the Views min / max text fields are still there and when I choose anything from the dropdown and hit apply, nothing gets filtered out. Am I missing something? Is the name of my exposed element wrong? Plus I guess my way would be different, since I have two textfields and each should become a select list (minimum value would be selected on the left and maximum value on the right). If your way worked though, I wouldn't mind using it. Do you think your way would even be the right strategy for my exposed filter? Its operator is an "is between", which adds min and max text fields.

I managed to write a module

aschiwi's picture

I managed to write a module that turns my exposed filter "between" fields from a text field into select lists. "alter" is the fields name, it's German for age. Here is the code which seems to work fine at first sight:

<?php
function agerange_form_views_exposed_form_alter(&$form, $form_state) {



  if ($form['#id'] == 'views-exposed-form-bday-page-2') {



    $form['alter']['min']['#type'] = 'select';
    $form['alter']['min']['#size'] = 1;
    $form['alter']['min']['#options'] = drupal_map_assoc(range(18, 99));
 
   $form['alter']['max']['#type'] = 'select';
  $form['alter']['max']['#size'] = 1;
    $form['alter']['max']['#options']  = drupal_map_assoc(range(18, 99));

    $form['#submit'][] = 'agerange_form_views_exposed_form_alter_submit';
  }
}

duellj, thank you again for helping me out with this.

Anja

hi, thanx a lot for such a

czeky's picture

hi, thanx a lot for such a great examples, I'm trying to override exposed filter operators and limit them only to "is less than or equal" and "is greater than or equal". so far I managed (based on this and other tutorials) how to identify and partially override my form, my problem (and the question) is..

<?php
$form
['#info']['filter-title']['label'] = t('Label'); // working fine
?>

but when I just try to reach cck fields like this..

<?php
$form
['bd']['#default_value'] = t('Default value'); // does not work
?>

according to dsm($form) it should be fine, bd is an input field

when I'll solve this, it should be easy to override array of exposed operators like..

<?php
$form
['field_dim_d_value_op'] = array(
     
'#options' => array(<=  => 'is less than or equal', >= => 'is greater than or equal'),
    );
?>

thanx for a help

got it working here

czeky's picture

got it working here http://drupal.org/node/716420

Yes, thanks to duellj for

Sansui's picture

Yes, thanks to duellj for some great info on this page :) Here's how I did my own form modification for a price range, with two different select lists to replace the "between" values text fields. I was having trouble getting the single select range to work with "between", but this worked great for two select boxes, and didn't need an extra function for submit.

<?php


    $form
['field_currentprice_value']['min']['#type'] = 'select';
   
$form['field_currentprice_value']['min']['#attributes'] = array (
     
'size' => 1,
      );
   
$form['field_currentprice_value']['min']['#options'] = array(
     
'10000' => '10K',
     
'50000' => '50K',
     
'75000' => '75K',
     
'100000' => '100K',
     
'125000' => '125K',
     
'150000' => '150K',
     
'175000' => '175K',
     
'200000' => '200K',
     
'225000' => '225K',
     
'250000' => '250K',
     
'300000' => '300K',
     
'350000' => '350K',
     
'400000' => '400K',
    );

   
$form['field_currentprice_value']['max']['#type'] = 'select';
   
$form['field_currentprice_value']['max']['#attributes'] = array (
     
'size' => 1,
      );
   
$form['field_currentprice_value']['max']['#options'] = array(
     
'10000' => '10K',
     
'50000' => '50K',
     
'75000' => '75K',
     
'100000' => '100K',
     
'125000' => '125K',
     
'150000' => '150K',
     
'175000' => '175K',
     
'200000' => '200K',
     
'225000' => '225K',
     
'250000' => '250K',
     
'300000' => '300K',
     
'350000' => '350K',
     
'400000' => '400K',
     
'5000000' => '5MIL',
    );
?>

Cannot extract ranges from URL

pabx's picture

I've got a similar problem. I've followed the suggestion above, and have replaced the Minimum and Maximum fields with a single dropdown menu. However, the submit function only seems to spit back out the value that I've listed in the options. So instead of the URL ending in:

&field_main_sq_feet_value%5Bmin%5D=2000&field_main_sq_feet_value%5Bmax%5D=3999

it ends in:

&field_main_sq_feet_value=2000-3999

Any suggestions? My module code is below.

<?php
// $Id$

function mytools_form_views_exposed_form_alter(&$form, $form_state) {
  if (
$form['#id'] == 'views-exposed-form-house-list-page-1') {
   
$form['field_main_sq_feet_value']['#type'] = 'select';
   
$form['field_main_sq_feet_value']['#options'] = array(
     
'' => 'any',
     
'1-1999' => '1-1999 sq.ft',
     
'2000-2999' => '2000-2999 sq.ft',
     
'3000-3999' => '3000-3999 sq.ft',
     
'4000-4999' => '4000-4999 sq.ft',
     
'5000-10000' => '5000-10000 sq.ft',
    );
   
$form['#submit'][] = 'mytools_form_views_exposed_form_alter_submit';
  }
}

function
mytools_form_views_exposed_form_alter_submit(&$form_id, &$form_values) {
  if (
$form_state['values']['field_main_sq_feet_value']) {
    list(
$values['min'], $values['max']) = explode("-", $form_state['values']['field_main_sq_feet_value']);
   
$form_state['view']->exposed_data['field_main_sq_feet_value'][0]['value'] = $values;
  }
}
?>

Your problem is probably long

timd.mackey's picture

Your problem is probably long past, but I wsa running into the same problem and thought I would offer the solution. It appears that a typo in duellj's original code was causing it. the submit function was missing a reference to $form_state and instead had $form referenced twice.

What he wrote as this:
<?php function mymodule_form_views_exposed_form_alter_submit(&$form, &$form) { ?>

Should actually be:
<?php function mymodule_form_views_exposed_form_alter_submit(&$form, &$form_state) { ?>

Very helpful and a validation fix.

jbylsma's picture

First off, thanks a ton! I was going the complicated route of creating a new filter handler when I stumbled across this. I did run into a validation problem though, and was stumped for a while trying to solve it. After some hunting, I figured out I was getting the "An illegal choice has been detected. Please contact the site administrator." error because the exposed filter was still expecting an array of values. In order to around it, I added the following below $form['submit'] in the hook_form_alter.

<?php
if (is_array($form_state['input']['exposed_filter'])) {
  unset(
$form_state['input']['exposed_filter']);
}
?>

Afterward, everything was working as expected. Again, thanks for your help!

Illegal choice

helenj's picture

I followed your advice but found I needed to put the unset code in the hook_form_alter before the $form['submit'], as I was getting the error when the page was loading. Solved my problem though, thanks for the tip!

Would you mind pasting the

zdean's picture

Would you mind pasting the final hook_form_alter code you used? I've tried pasting the unset code in every imaginable place and I still get the Illegal Choice error.

Thanks!

hook_form_alter

helenj's picture

Hi zdean,

Here is the hook_form_alter that I am using. I found that I didn't actually need to include a call to _form_alter_sbumit(), and everything seems to be working well now. I hope this helps you.

<?php
function properties_search_form_alter(&$form, $form_state, $form_id) {

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

   
$price_min_options = array(
     
'0'     => t('no minimum price'),
     
'750'   => '750',
     
'1000'  => '1 000',
     
'1500'  => '1 500',
    );
   
$price_max_options = array(
     
'10000' => t('no maximum price'),
     
'750'   => '750',
     
'1000'  => '1 000',
     
'1500'  => '1 500',
     
'2000'  => '2 000',
    );
 
$form[$field_name.'_value']['min'] = array (
   
'#type'           => 'select',
   
'#multiple'       => false,
   
'#required'       => false,
   
'#options'        => $price_min_options,
   
'#default_value'  => 'No Min',
  );     
 
$form[$field_name.'_value']['max'] = array (
   
'#type'           => 'select',
   
'#multiple'       => false,
   
'#required'       => false,
   
'#options'        => $price_max_options,
   
'#default_value'  => 'No Max',
  ); 

}
?>

Thank you! That helps a lot!

zdean's picture

Thank you! That helps a lot!

drupalina's picture

Thanks for the code. It works almost perfectly in D7.

But for some strange reason it adds "No min" and "No max" options on every other form, including admin configuration forms and node creation forms, which I assume will be very confusing to the end users.
(In addition it does not allow values higher than 10,000, and if Min amount of lets say 2500 is selected, then all the values below 2500 in the Max field should disappear, otherwise it creates a contradiction).

Other than that, just wanted to confirm that it works in D7.

In Drupal 6, I believe it is

eliza411's picture

In Drupal 6, I believe it is the difference between selecting the Allowed values list and the other option.

For example, I have a field called: School (field_school)

It has two filter choices:

School (field_school)
School (field_school) - Allowed values

The first gives a text field when exposed, the second a pick list.

Oh by ghod, it is that

grantkruger's picture

Oh by ghod, it is that simple. smacks forehead

I did not even see that option. Thanks so much. Views-love still going strong.

Sala kahle,
Grant

Same here! doh

norio's picture

Same here! doh

Oh snap! Thanks!

R.J. Steinert's picture

Oh snap! Thanks!

Thanks

catya's picture

This just solved my same problem many months later. Thanks!

Views Fields

bioborg's picture

Am I missing a module that lets me have cck fields in my views filters? Because I see no option for this, I just get Node: Title and Node: Body fields to select from in my filters. All the cool fields in my custom content types don't show up in the filters.

Content Group

duellj's picture

All CCK fields show up in the "Content" group. So if you have a field named "Text Field" it will show up in the filters list as "Content: Text Field (field_text_field)".

Is it my turn to be missing

TechnoBuddhist's picture

Is it my turn to be missing something now?

In Views I can see my field in the Content: section, but I don't see the " - Allowed values" bit.

I see;

School (field_school)

and not

School (field_school)
School (field_school) - Allowed values

I have allowed options set in cck and I get the dropdown list when editing my node, so what am I missing?

Please ignore my post above,

TechnoBuddhist's picture

Please ignore my post above, I found it. I was looking in the wrong place. I want a dropdown to be displayed in the view and don't want the dropdown as a filter.

I need what Anitha Rajesh is requesting

drecute's picture

Hello, I need just exactly what Anitha Rajesh is making request for. When you create a cck textfield, views doesn't provide options to turn this to dropdown. Like I want the values entered by users to fill that dropdown select. So my take is if I write the form_alter code like this

<?php
function xx_form_views_exposed_form_alter(&$form, $form_state) {



  if (
$form['#id'] == 'views-exposed-form-price-range-page-1') {



   
$form['alter']['min']['#type'] = 'select';
   
$form['alter']['min']['#size'] = 1;
   
$form['alter']['min']['#options'] = drupal_map_assoc(range(18, 99));

  
$form['alter']['max']['#type'] = 'select';
 
$form['alter']['max']['#size'] = 1;
   
$form['alter']['max']['#options']  = drupal_map_assoc(range(18, 99));

   
$form['#submit'][] = 'xx_form_views_exposed_form_alter_submit';
  }
}
?>

How do I allow the options to be the cck textfield values instead of static code like

<?php
drupal_mac_assoc
(range(18, 99));
?>
as indicated by aschiwi.

t: @charyorde

Maybe one could query the

gosta100's picture

Maybe one could query the database in order to populate the options of the dropdown list. I used the following code to make a view's exposed filter (city) a dropdown list showing all the (distinct) cities entered in the Location of different nodes.

<?php
function mymodule_form_views_exposed_form_alter(&$form, $form_state) {
  if (
$form['#id'] == 'views-exposed-form-location-table-page-1') {
   
$result = db_query('SELECT DISTINCT(l.city) FROM {location} l ORDER BY l.city ASC');
    while (
$row = db_fetch_array($result))
     
$options[$row['city']] = $row['city'];
     
$form['city'] = array(
     
'#type' => 'select',
     
'#options' => $options,
    );
  }
}
?>

Wow, this code works great!

Zehrious's picture

Wow, this code works great! But, how can I set the first result in the array to be a static value and have the rest of the results be dynamic? I need the first item in the select list to be...

'' => '<Any>',

Thanks!

It takes a lot of research

Zehrious's picture

It takes a lot of research for a non-programmer like me to do something so simple. But, it's very rewarding.

<?php
function mymodule_form_views_exposed_form_alter(&$form, $form_state) {
  if (
$form['#id'] == 'views-exposed-form-location-table-page-1') {
   
$result = db_query('SELECT DISTINCT(l.city) FROM {location} l ORDER BY l.city ASC');
    while (
$row = db_fetch_array($result))
     
$options[$row['city']] = $row['city'];
     
$option1[""] = "<Any>";
     
$form['city'] = array(
     
'#type' => 'select',
     
'#options' => array_merge($option1, $options),
    );
  }
}
?>

array array_merge  ( array $array1  [, array $array2  [, array $...  ]] )

Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.

If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.

Values in the input array with numeric keys will be renumbered with incrementing keys starting from zero in the result array.

My first Module

Raj_mbox's picture

Spent 2 weeks to get drop down list for view's exposed filter using default view arguments and relation- but failed. I think need to incorporate this code, but how? My view's exposed filter field name is Content: Program (field_program). The table name is "content_type_program" and field in the table is "field_program_value".
How should I update the code

<?php
function mymodule_form_views_exposed_form_alter(&$form, $form_state) {
  if (
$form['#id'] == 'views-exposed-form-location-table-page-1') {
   
$result = db_query('SELECT DISTINCT(l.city) FROM {location} l ORDER BY l.city ASC');
    while (
$row = db_fetch_array($result))
     
$options[$row['city']] = $row['city'];
     
$form['city'] = array(
     
'#type' => 'select',
     
'#options' => $options,
    );
  }
}
?>

What should I write inplace of:

views-exposed-form-location-table-page :
l.city :
location :

Anything else that needed to be replaced

I could find that I have to create folder C:\wamp\www\sites\all\Modules\Custom\filterselectlist
in the folder have to create filterselectlist.info file and place above code in filterselectlist.module file
after that need to enabe filterselectlist module

Request for urgent help!

Thanks in advance

My First Module

Raj_mbox's picture

I have updated the code as follows

<?php
function mymodule_form_views_exposed_form_alter(&$form, $form_state) {
  if (
$form['#id'] == 'views-exposed-form-location-table-page-1') {
   
$result = db_query('SELECT DISTINCT(P.field_program_value) FROM {content_type_program} P ORDER BY P.field_program_value ASC');
    while (
$row = db_fetch_array($result))
     
$options[$row['field_program_value']] = $row['field_program_value'];
     
$form['field_program_value'] = array(
     
'#type' => 'select',
     
'#options' => $options,
    );
  }
}
?>

I have enabled the module at http://localhost/?q=admin/build/modules

But still I am not getting select list as exposed filter view

Select or other cck

lina.irshaid's picture

how to set the allowed values for other option that i have chosen in the list????

allowed values

Raj_mbox's picture

When you Add Content Type and add a Text Field and Select List Widget on the next screen in very last option you need to specify the Allowed Values like
One
Two
Three
Four

but this is hard coded list.

Now when you create view and expose filter on this field you will see option as you mentioned above

School (field_school)
School (field_school) - Allowed values

Offcourse in place of School (field_school) there will be Text Field created by you

What I am looking for is select list widget as exposed filter for a text field in which values entered by user!

My solution: <?phpfunction

OFF's picture

My solution:

<?php

function mymodule_form_views_exposed_form_alter(&$form, $form_state) {
  if (
$form['project_client']) {
   
$form['project_client']['#type'] = 'select';
  
$form['project_client']['#attributes'] = array (
     
'size' => 1,
    );
   
$form['project_client']['#options'] = array(
     
'' => t('<Any>'),
     
'0' => 'Собственный',
     
'1' => 'Клиент',
    );
   
$form['#submit'][] = 'mymodule_form_views_exposed_form_alter_submit';
  }
}
?>

Don't forget forget to

pasive's picture

Don't forget forget to add

<?php
if ($form_state['view']->name = 'view_name') {
}
?>

otherwise you might have it on all views

you mean <?phpif

jrz's picture

you mean

<?php
if ($form_state['view']->name == 'view_name') {
}
?>

Hi, My code is working if I

pmathur01's picture

Hi,

My code is working if I am trying to just modify a single form element. What do I need to change to modify multiple elements. Here I have field_1 which is modified. Now what if I have another field Field_2 that is to be modified in the same form.
How do I go about that.??

Thanks,
Prerna

<?
/**
* Implementation of hook_form_alter().
*/
function custom_filter_form_alter(&$form, $form_state, $form_id) {
// Load up a $view object like we would expect to work with
if (isset($form['#parameters'][1]['view'])) {
$view = $form['#parameters'][1]['view'];
}
switch ($form_id) {

case 'views_exposed_form':

  if ($view->name == 'view_search_block') {

    custom_filter_range_to_select('Field_1', array(
       '0,50000' => '$0-$50,000',
       '50000,100000' => '$50,000-$100,000',
      '100000,200000' => '$100,000-$200,000',
      '200000,300000' => '$200,000-$300,000',
      '300000,400000' => '$300,000-$400,000',
      '400000,500000' => '$400,000-$500,000',
    ), $form, $form_state);

      break;
}
}

}

function custom_filter_range_to_select($field, $options, &$form,
&$form_state, $optional = TRUE) {
$form[$field]['#type'] = 'select';
if ($optional) {
$options = array_merge(array('All' => ''), $options);
}
$form[$field]['#options'] = $options;
unset($form[$field]['min']);
unset($form[$field]['max']);
$f = $form_state['input'][$field];
$f ? $form[$field]['#value'] = $f : true;
$form[$field]['#element_validate'] = array('custom_filter_range_validate');
}

function custom_filter_range_validate($element, &$form_state) {
if (($v = $element['#post'][$element['#name']])) {
if ($v == 'All') {

  $min = $max = '';
}
else {
  list($min, $max) = explode(',', $v);
}
$form_state['input'][$element['#name']] = array(
  'min' => $min,
  'max' => $max,
);


$form_state['values'][$element['#name']] = array(
  'min' => $min,
  'max' => $max,
);

}
}

submit issue

luk82's picture

Hi,

I used this code to alter my view. sell_price field is being altered but when I click submit 'mymodule_form_views_exposed_form_alter_submit' is not executed. Basically view form is not altered again when i click submit. Any ideas why?

function mymodule_form_views_exposed_form_alter(&$form, &$form_state) {
if ($form['#id'] == 'form_#id') {
$form['sell_price']['#type'] = 'select';
$form['sell_price']['#options'] = array(
'0-10' => '£0-£10',
'20-50' => '£20-£50',
'50-100' => '£50-£100',
'100-10000000' => '£100+',
);
$form['#submit'][] = 'mymodule_form_views_exposed_form_alter_submit';
}
}

function mymodule_form_views_exposed_form_alter_submit(&$form, &$form_state) {
if ($form_state['values']['sell_price']) {
$exposed = explode("-", $form_state['values']['sell_price']);
$values = array($exposed['min'] => $exposed[0], $exposed['max'] => $exposed[1]);
if ($form_state['view']->name == 'view_name') {
$form_state['view']->exposed_data['sell_price'] = $values;
}
}
}

Thanks for duellj.

husumiao's picture

The solutions by duellj have provider is worked. But we have another module can do this job on D7?

Can someone help to re-write this for D7 ?

drupalina's picture

Following the examples in this thread I created this module called "Selectable Price", so as to create a dropdown price range filter as usually seen on property listing websites. But unfortunately it didn't work!

<?php
function selectable_price_form_views_exposed_form_alter(&$form, $form_state) {
  if (
$form['#id'] == 'views-exposed-form-properties_to_rent-page') {
   
$form['field_rentpcm_value']['min']['#type'] = 'select';
  
$form['field_rentpcm_value']['min']['#attributes'] = array (
     
'size' => 1,
      );
   
$form['field_rentpcm_value']['min']['#options'] = array(
     
'100' => '£100pcm',
     
'200' => '£200pcm',
     
'300' => '£300pcm',
     
'400' => '£400pcm',
     
'500' => '£500pcm',
     
'600' => '£600pcm',
     
'700' => '£700pcm',
     
'800' => '£800pcm',
     
'900' => '£900pcm',
    );
   
$form['#submit'][] = 'selectable_price_form_views_exposed_form_alter_submit';
  }
    if (
$form['#id'] == 'views-exposed-form-properties_to_rent-page') {
   
$form['field_rentpcm_value_1']['max']['#type'] = 'select';
   
$form['field_rentpcm_value_1']['max']['#attributes'] = array (
     
'size' => 1,
      );
   
$form['field_rentpcm_value_1']['max']['#options'] = array(
     
'100' => '£100pcm',
     
'200' => '£200pcm',
     
'300' => '£300pcm',
     
'400' => '£400pcm',
     
'500' => '£500pcm',
     
'600' => '£600pcm',
     
'700' => '£700pcm',
     
'800' => '£800pcm',
     
'900' => '£900pcm',
    );
   
$form['#submit'][] = 'selectable_price_form_views_exposed_form_alter_submit';
  }

}
function
selectable_price_form_views_exposed_form_alter_submit(&$form, &$form_state) {
  if (
$form_state['values']['field_rentpcm_value']) {
    list(
$values['min'], $values['max']) = explode("-", $form_state['values']['field_rentpcm_value']);
   
$form_state['view']->exposed_data['field_rentpcm_value'] = $values;
  }
    if (
$form_state['values']['field_rentpcm_value_1']) {
    list(
$values['min'], $values['max']) = explode("-", $form_state['values']['field_rentpcm_value_1']);
   
$form_state['view']->exposed_data['field_rentpcm_value_1'] = $values;
  }
}
?>

(minus the last "?>" to make it proper module)

Either I did something wrong in the code (I'm not a coder), or maybe things have changed for D7.
Can someone please help write this for D7?

have you solved? I have the

laraz's picture

have you solved? I have the same issue.

Thanks.

Thank you helenj ....

wooody's picture

Thank you helenj ....