Imagine, that you have float field with 2 symbols after dot.
At the views page with filter you need to filter values by all range of module of user's float input.
Ex: user input 2.45, we filter all values  X>=2 AND X<(2+1)

With the standard accessories of views you can filter with two filter “from” and “to”. But in FO we have only one field.
To solve this problem we can use hook_views_query_alter.

We already have a view. To make our hook work, we must set the unique tag to the view, to check it in out hook. mymodule_query in example code.

function mymodule_fix_views_query_alter(&$view, &$query){
  $fields = array('field_data_field_diameter.field_diameter_value');
  if (!empty($query->options) && !empty($query->options['query_tags']) && in_array('mymodule_query', $query->options['query_tags'])) {
    foreach ($query->where as $ckey1 => &$cond_key) {
      foreach ($cond_key['conditions'] as $ckey => &$condition) {
        if (in_array($condition['field'], $fields)) {
          $value = $condition['value'];
          $value_start = (int) $value;
          $value_end = $value_start + 1;

          $condition['value'] = $value_start;
          $condition['operator'] = '>=';

          $condition_new = array();
          $condition_new['field'] = $condition['field'];
          $condition_new['value'] = $value_end;
          $condition_new['operator'] = '<';
          $query->where[$ckey1]['conditions'][] = $condition_new;

$fields – is arrat with fields we want to replace the values “from” and “to”.

If tag is equal with tagged views – apply replaced values.

In this case, if user would filter by 6.7 value, out algorithm will find field values in the ranfe form 6 to 7 (no incl)

Add new comment

The content of this field is kept private and will not be shown publicly.
  • No HTML tags allowed.
 dP   dP  dP   dP   dP   a88888b.  .d888b.   888888ba  
88 88 88 88 88 d8' `88 Y8' `8P 88 `8b
88aaa88 88 .8P .8P 88 d8bad8b a88aaaa8P'
88 88 d8' d8' 88 88` `88 88 `8b.
88 88.d8P8.d8P Y8. .88 8b. .88 88 .88
dP 8888' Y88' Y88888P' Y88888P 88888888P

Enter the code depicted in ASCII art style.