Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Views: Date filter does not work with dynamic dates. #2625

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions core/modules/views/handlers/views_handler_filter_date.inc
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,10 @@ class views_handler_filter_date extends views_handler_filter_numeric {
}

function op_between($field) {
$a = intval(strtotime($this->value['min'], 0));
$b = intval(strtotime($this->value['max'], 0));
// Use the substitutions to ensure a consistent timestamp.
$query_substitutions = views_views_query_substitutions($this->view);
$a = intval(strtotime($this->value['min'], $query_substitutions['***CURRENT_TIME***']));
$b = intval(strtotime($this->value['max'], $query_substitutions['***CURRENT_TIME***']));

if ($this->value['type'] == 'offset') {
$a = '***CURRENT_TIME***' . sprintf('%+d', $a); // keep sign
Expand All @@ -172,10 +174,10 @@ class views_handler_filter_date extends views_handler_filter_numeric {
}

function op_simple($field) {
$value = intval(strtotime($this->value['value'], 0));
if (!empty($this->value['type']) && $this->value['type'] == 'offset') {
$value = '***CURRENT_TIME***' . sprintf('%+d', $value); // keep sign
}
// Use the substitutions to ensure a consistent timestamp.
$query_substitutions = views_views_query_substitutions($this->view);
$value = intval(strtotime($this->value['value'], $query_substitutions['***CURRENT_TIME***']));

// This is safe because we are manually scrubbing the value.
// It is necessary to do it this way because $value is a formula when using an offset.
$this->query->add_where_expression($this->options['group'], "$field $this->operator $value");
Expand Down
32 changes: 31 additions & 1 deletion core/modules/views/tests/handlers/views_handler_filter_date.test
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@ class ViewsHandlerFilterDateTest extends ViewsSqlTest {
$this->assertIdenticalResultset($view, $expected_result, $this->map);
$view->destroy();

// Test "first day of" type of relative dates for simple operator.
$view->set_display('default');
$view->init_handlers();
$view->filter['created']->operator = '<';
$view->filter['created']->value['type'] = 'offset';
$view->filter['created']->value['value'] = 'last day of January 1970';
$view->execute_display('default');
$expected_result = array(
array('nid' => $this->nodes[0]->nid),
array('nid' => $this->nodes[1]->nid),
array('nid' => $this->nodes[2]->nid),
);
$this->assertIdenticalResultset($view, $expected_result, $this->map);
$view->destroy();

// Test offset for between operator.
$view->set_display('default');
$view->init_handlers();
Expand All @@ -60,8 +75,23 @@ class ViewsHandlerFilterDateTest extends ViewsSqlTest {
);
$this->assertIdenticalResultset($view, $expected_result, $this->map);
$view->destroy();
}

// Test "first day of" type of relative dates for between operator.
$view->set_display('default');
$view->init_handlers();
$view->filter['created']->operator = 'between';
$view->filter['created']->value['type'] = 'offset';
$view->filter['created']->value['max'] = 'last day of January 1970';
$view->filter['created']->value['min'] = 'first day of January 1970';
$view->execute_display('default');
$expected_result = array(
array('nid' => $this->nodes[0]->nid),
array('nid' => $this->nodes[1]->nid),
array('nid' => $this->nodes[2]->nid),
);
$this->assertIdenticalResultset($view, $expected_result, $this->map);
$view->destroy();
}

/**
* Tests the filter operator between/not between.
Expand Down