Closed (fixed)
Project:
Views (for Drupal 7)
Version:
7.x-3.7
Component:
exposed filters
Priority:
Normal
Category:
Support request
Assigned:
Unassigned
Reporter:
Created:
16 Apr 2013 at 20:57 UTC
Updated:
19 Apr 2013 at 16:05 UTC
I'm a bit clueless as to what I'm doing here but this is what I have currently...
/**
* Generic views handler filter to add code to manipulate the query object.
*/
class drfilter_handler_filter extends views_handler_filter_numeric {
function can_expose() {
return TRUE;
}
/**
* Provide a list of all operators.
*/
function fields_operator_options() {
return array(
'-' => t('minus'),
'+' => t('plus'),
);
}
/**
* Overrides views_handler_filter#options_form().
*/
function options_form(&$form, &$form_state) {
parent::options_form($form, $form_state);
$form['operator'] = array(
'#type' => 'select',
'#title' => t('Operator'),
'#default_value' => $this->options['operator'],
'#options' => $this->fields_operator_options(),
'#weight' => -2,
);
}
/**
*
*/
function pre_render(&$values) {
$s = $values[0]->field_data_field_ol_locator_geofield_field_geofield_distance;
$r = $values[0]->field_field_radius[0]['raw']['value'];
switch ($this->options['operator'])
{
case '-':
$this->value['min'] = $s - $r;
$this->value['value'] = $s - $r;
$this->value['max'] = $s - $r;
break;
case '+':
$this->value['min'] = $s + $r;
$this->value['value'] = $s + $r;
$this->value['max'] = $s + $r;
break;
default:
break;
}
}
/**
* Overrides views_handler_filter#admin_summary().
*/
function admin_summary() {
return check_plain(
$this->options['operator']
);
}
}
Now being naive to the ways of views, this is how I'm expecting this to work.
1. I have the filter exposed with grouped filters.
2. When I set $this->value['value'] = $x; I'm expecting that the numerical field will be evaluated against $x and if the condition (e.g. > 5) is not satisfied then the row will be removed from the results.
Am I totally in the wrong domain here, in trying to achieve what I want?
Comments
Comment #1
rudiedirkx commentedThis filter doesn't do anything. If
pre_render()is executed (I don't think filter handlers have apre_render()?) it only sets$this->value. It never uses that value.Are you trying to manipulate the db query? You'll need to override
handler::query()to manipulate$this->query, probably using the operator and stuff from your geofield and redius field.Most Views filter handlers will use query() to manipulate the db query to limit the results. It's possible to limit the results after executing the query, but that's probably not the most efficient method. If you know what the filter should add to the query (what the SQL looks like), we could go from there.
Comment #2
el_toro commentedYes the pre_render() does execute but, as you say, has no effect... I'm not sure how I could manipulate the query as the distance value ($s) is not present in the database but is computed for each result (after being retrieved from the database).
EDIT: The distance is computed relative to a point that the user specifies in another filter.
Comment #3
rudiedirkx commentedIf you don't want to manipulate the query, what do you want the filter to do? Your code doesn't really explain... Do you want to filter results based on the distance? If you can't do that in the db, you can use
post_execute()to filter$this->view->result. I think.Comment #4
el_toro commentedHey,
I was able to get it working. The formula I was trying to execute to filter was (Distance - Radius) < x. I ended up using a combination of these filters: geofield_handler_filter, views_handler_filter_fields_compare and views_filters_populate_handler_filter.
Here is what the code looks like:
Thanks for taking time to help me on this! I appreciate it.
Comment #5
rudiedirkx commentedNo problem.