Created
February 14, 2011 23:48
-
-
Save vosechu/826826 to your computer and use it in GitHub Desktop.
Functions and implementation of Views exposed range selector filter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name = Views Tweaks | |
description = Misc tweaks to Views | |
core = 6.x | |
dependencies[] = views |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Implementation of hook_form_alter(). | |
*/ | |
function views_tweaks_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 == 'products_wall') { | |
views_tweaks_range_to_select('field_price_value', array( | |
'0,999' => 'Less than $1,000', | |
'1000,1999' => '$1,000–$1,999', | |
'2000,3999' => '$2,000–$3,999', | |
'4000,6999' => '$4,000–$6,999', | |
'7000,999999' => '$7,000 plus', | |
), $form, $form_state); | |
} | |
break; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* 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 <any> value at the top of | |
* the select dropdown. Defaults to TRUE. | |
*/ | |
function views_tweaks_range_to_select($field, $options, &$form, &$form_state, $optional = TRUE) { | |
$form[$field]['#type'] = 'select'; | |
if ($optional) { | |
$options = array_merge(array('All' => '<Any>'), $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('views_tweaks_range_validate'); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* 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_tweaks_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, | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment