public static function ContextualRangeFilter::buildRangeQuery in Views Contextual Range Filter 8
Build a range query based on the ranges passed in.
Parameters
object $views_argument_plugin: The View's contextual filter plugin.
string $field: The full field name as used in the SQL statement, or NULL.
object $range_converter: Optional function to convert the from--to range, e.g., for relative date ranges like "-3 months--last week" to be converted to "20200120--20200413".
3 calls to ContextualRangeFilter::buildRangeQuery()
- DateRange::query in src/
Plugin/ views/ argument/ DateRange.php - Prepare the range query where clause.
- NumericRangeArgument::query in src/
Plugin/ views/ argument/ NumericRangeArgument.php - Create the query.
- StringRangeArgument::query in src/
Plugin/ views/ argument/ StringRangeArgument.php - Build the query.
File
- src/
ContextualRangeFilter.php, line 50
Class
- ContextualRangeFilter
- Functions for the contextual_range_filter module.
Namespace
Drupal\contextual_range_filterCode
public static function buildRangeQuery($views_argument_plugin, $field = NULL, $range_converter = NULL) {
if (!isset($views_argument_plugin) || $views_argument_plugin->value === FALSE) {
return;
}
$real_field = $views_argument_plugin->realField;
if (!isset($field)) {
// Example: "node__field_price.field_price_value".
$field = "{$views_argument_plugin->tableAlias}.{$real_field}";
}
// $is_not comes from "Exclude" tickbox.
$is_not = !empty($views_argument_plugin->options['not']);
$null_check = $is_not ? "OR {$field} IS NULL" : '';
// All WHERE clauses are OR-ed or AND-ed together in the same group.
// Note: NOT (a OR b OR c) == (NOT a) AND (NOT b) AND (NOT c).
$group = $views_argument_plugin->query
->setWhereGroup($is_not ? 'AND' : 'OR');
foreach ($views_argument_plugin->value as $range) {
$placeholder = $views_argument_plugin->query
->placeholder($real_field);
list($from, $to) = self::split($range);
if (is_callable($range_converter)) {
list($from, $to) = call_user_func($range_converter, $from, $to);
}
if ($to === FALSE) {
// Dealing with a single value, not a range.
$operator = $is_not ? '!=' : '=';
$views_argument_plugin->query
->addWhereExpression($group, "{$field} {$operator} {$placeholder} {$null_check}", [
$placeholder => $range,
]);
}
elseif ($from != '' && $to != '') {
// from--to.
$operator = $is_not ? 'NOT BETWEEN' : 'BETWEEN';
$placeholder_from = $placeholder;
$placeholder_to = $views_argument_plugin->query
->placeholder($real_field);
$args = [
$placeholder_from => $from,
$placeholder_to => $to,
];
$expression = "{$field} {$operator} {$placeholder_from} AND {$placeholder_to} {$null_check}";
$views_argument_plugin->query
->addWhereExpression($group, $expression, $args);
}
elseif ($from != '') {
// from--.
$operator = $is_not ? '<' : '>=';
$views_argument_plugin->query
->addWhereExpression($group, "{$field} {$operator} {$placeholder} {$null_check}", [
$placeholder => $from,
]);
}
elseif ($to != '') {
// --to
$operator = $is_not ? '>' : '<=';
$views_argument_plugin->query
->addWhereExpression($group, "{$field} {$operator} {$placeholder} {$null_check}", [
$placeholder => $to,
]);
}
}
}