方法一
通过使用hook_views_query_alter()。请参见以下示例:
<?php
/**
* Implements hook_views_query_alter().
*/
function foo_views_query_alter(&$view, &$query) {
if ($view->name == 'foo_view') {
// Allow any distance when the postcode it is not specified.
if (empty($_GET['postcode']['postal_code']) || $_GET['postcode']['postal_code'] === 'All') {
// Scan through the query.
foreach ($query->where as $condition_group_key => &$condition_group) {
foreach ($condition_group['conditions'] as $condition_key => &$condition) {
$search_name = '(COALESCE(ACOS(';
if (is_string($condition['field']) && strstr($condition['field'], $search_name) !== FALSE) {
// Remove filter from the query.
unset($query->where[$condition_group_key]['conditions'][$condition_key]);
}
} // end: foreach
} // end: foreach
} // end: if
/*
* Change the field conditions.
* Possible field values: 1, 2, 3
* Logic: When 3 is selected, then display 1, 2 and 3.
*/
switch (@$view->display_handler->handlers['filter']['field_123_value']->value[0]) {
case 3:
foreach ($query->where as $condition_group_key => &$condition_group) {
foreach ($condition_group['conditions'] as $condition_key => &$condition) {
if($condition['field'] == 'field_data_field_123.field_123_value') {
unset($query->where[$condition_group_key]['conditions'][$condition_key]);
$query->where[] = array(
'conditions' => array(
array(
'field' => 'field_data_field_123.field_123_value',
'value' => 1,
'operator' => "=",
),
array(
'field' => 'field_data_field_123.field_123_value',
'value' => 2,
'operator' => "=",
),
array(
'field' => 'field_data_field_123.field_123_value',
'value' => 3,
'operator' => "=",
),
),
'args' => array(),
'type' => 'OR',
);
}
}
} // end: foreach
break;
} // end: switch
} // end: if
}
方法2
通过使用hook_views_pre_execute和自定义函数来查看示例,该函数试图找到正确的字段条件并返回对该条件的引用:
/**
* Implements hook_views_pre_execute().
*/
function foo_views_pre_execute(&$view) {
if ($view->name == 'foo_view') {
foo_get_view_filter_recursively(
$view,
$view->build_info['query']->conditions(),
'field_data_field_123.field_123_value',
$filter
);
// We want our filter to work as a bit mask.
$filter[0]['operator'] = '&';
unset ($filter);
// Example of finding Proximity filter condition
$search_name = '(COALESCE(ACOS(';
foo_get_view_filter_recursively(
$view,
$view->build_info['query']->conditions(),
$search_name,
$filter
);
if (empty($_GET['postcode']['postal_code']) || $_GET['postcode']['postal_code'] === 'All') {
// Allowing any distance.
$filter[0]['value'][':distance'] = 10000000;
}
else {
$filter[0]['value'][':distance'] = 80000;
}
unset ($filter);
// Fetching single record?
foo_get_view_filter_recursively(
$view,
$view->build_info['query']->conditions(),
'node.nid',
$filter
);
if (!empty($_GET['nid'])) {
$filter[0]['value'] = (int) $_GET['nid'];
}
else {
$filter[0]['operator'] = '<>';
}
unset ($filter);
// echo '<pre style="font-size:11px;font-family: Monaco">'; print_r($view->build_info['query']); exit;
}
}
/**
* Custom function to find the field condition within the view
*/
function foo_get_view_filter_recursively($view, &$conditions, $field_name, &$filter) {
if (!empty($conditions)) {
foreach ($conditions as &$condition) {
if ($condition instanceof DatabaseCondition) {
if (foo_get_view_filter_recursively($view, $condition->conditions(), $field_name, $filter)) {
return TRUE;
}
} else if ($condition['field'] instanceof DatabaseCondition) {
if (foo_get_view_filter_recursively($view, $condition['field']->conditions(), $field_name, $filter)) {
return TRUE;
}
} elseif (is_string($condition['field']) && strstr($condition['field'], $field_name) !== FALSE) {
@$filter = array(&$condition);
return TRUE;
}
} // end: foreach
} // end: if
return FALSE;
}