Profile picture for Admin

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.
 d8888888P   .88888.     88888888b   888888ba   d888888P 
.d8' d8' `8b 88 88 `8b 88
.d8' 88 88 a88aaaa a88aaaa8P' 88
.d8' 88 db 88 88 88 88
d8' Y8. Y88P 88 88 88
Y8888888P `8888PY8b dP dP dP

Enter the code depicted in ASCII art style.