Skip to main content

How to make a range selector filter in Views

So you think you've figured out everything with drupal and views until you get the request from a client that just cannot be done via stock Views;What was the request about? Making a select box with a range of values where all you have is a CCK number/float.Wait,what?impossible you say?No,not of course , not at all ! It just needs a little bit of this and that and some magic dust and you are ready.Ok this is not something new or something i made it from scratch . I found here the solution (http://www.metaltoad.com/blog/how-make-range-selector-filter-views) by Chuck Vose but it is for drupal 6.x :) . . . Drupal 6.x you said ? yes , drupal 6.x and how about drupal 7.x ? well for drupal 7.x it's almost the same but you need to modify the new format of the 'form-fields' in drupal 7.x. So this is my solution for drupal 7.x hope it helps someone out there . name=='pages'){//check if the view is the PAGE views_ranger_range_to_select('field_weight_value', array( '1,2' => '1 to 2', '3,4' => '2.1 or 4', '5,8' => '5 to 8', '9,20' => '8.1 to 20', '21,999999' => '2.1 plus', ), $form, $form_state); }elseif($form_state['view']->name=='product_display'){ views_ranger_range_to_select('field_price_range_value', array( '0,3' => filter_xss('0.1 € έως 3.99 € '), '4,7' => filter_xss('4.00 € έως 7.00 €'), '8,15' => filter_xss('8.00 € έως 15 €'), '16,100' => filter_xss('Από 16€'), ), $form, $form_state); }else{} } } /** * Turn a range field into a select dropdown. This assumes that the $options array * is going to be something like: array('5,9' => '5 to 9 lbs') where the index is * a comma delimited string of the min/max values. * Pass in $optional = TRUE if you want there to be an value at the top of * the select dropdown. Defaults to TRUE. */ function views_ranger_range_to_select($field, $options, &$form, &$form_state, $optional = TRUE) { $form[$field]['#type'] = 'select'; $form[$field]['#default_value'] = 'All'; $form[$field]['#size'] = 'NULL'; 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; if(empty($form[$field]['#value'])){$form[$field]['#value']='All';} $form[$field]['#element_validate'] = array('views_ranger_range_validate'); } /** * Turn values created by range_to_select back into ranges so that Views can process * the request. This assumes that if the value passed in is 'All' the min/max array * should be set to array('min' => '', 'max' => '') */ function views_ranger_range_validate($element, &$form_state) { if (($v = $element['#value'])) { if ($v == 'All') { $min = $max = ''; }else { if(is_array($v)){$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, ); } } ?>
views drupal 7.x