Skip to content

Instantly share code, notes, and snippets.

@lsolesen
Created April 1, 2011 08:56
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lsolesen/897909 to your computer and use it in GitHub Desktop.
Save lsolesen/897909 to your computer and use it in GitHub Desktop.
Example on custom datetime filter for Views 3 for Drupal - see http://larsolesen.dk/node/273
core = "7.x"
name = "My Module"
description = "Example on how to add a filter handler with DATETIME"
package = "My modules"
php = "5.2.4"
project = "my_module"
version = "7.x-0.1"
files[] = "my_module_handler_filter_datetime.inc"
<?php
/**
* Implementation of hook_views_api().
*/
function vih_course_long_legacy_views_api() {
return array(
'api' => 3,
'path' => drupal_get_path('module', 'vih_course_long_legacy'),
);
}
<?php
/**
* Implementation of hook_views_data
*/
function my_module_views_data() {
// Define the base group of this table. Fields that don't
// have a group defined will go into this field by default.
$data['my_table']['table']['group'] = t('My table');
$data['my_table']['table']['base'] = array(
'field' => 'date',
'title' => t('My table'),
'help' => t('My table help description'),
'database' => 'my_database');
$data['my_table']['id'] = array(
'title' => t('Id'),
'help' => t('Id for the course'),
'field' => array('handler' => 'views_handler_field'),
'argument' => array('handler' => 'views_handler_argument_numeric'),
'filter' => array('handler' => 'views_handler_filter_numeric'),
'sort' => array('handler' => 'views_handler_sort_numeric'));
$data['my_table']['date_created'] = array(
'title' => t('Date created'),
'help' => t('Date for creation of post'),
'field' => array('handler' => 'views_handler_field'),
'argument' => array('handler' => 'views_handler_argument_date'),
'filter' => array('handler' => 'my_module_handler_filter_datetime'),
'sort' => array('handler' => 'views_handler_sort_date'));
return $data;
}
<?php
/**
* Custom filter handler for views, that handles DATETIME
*/
class my_module_handler_filter_datetime extends views_handler_filter_date {
function op_between($field) {
if ($this->operator == 'between') {
$a = intval(strtotime($this->value['min'], 0));
$b = intval(strtotime($this->value['max'], 0));
}
else {
$a = intval(strtotime($this->value['max'], 0));
$b = intval(strtotime($this->value['min'], 0));
}
if ($this->value['type'] == 'offset') {
// changed from original
$a = (integer)time() + (integer)sprintf('%+d', $a); // keep sign
$b = (integer)time() + (integer)sprintf('%+d', $b); // keep sign
// changed from original ends
}
// %s is safe here because strtotime scrubbed the input and we might
// have a string if using offset.
$this->query->add_where_expression($this->options['group'], "$field >= '".date("Y-m-d H:i:s", $a)."'");
$this->query->add_where_expression($this->options['group'], "$field <= '".date("Y-m-d H:i:s", $b)."'");
}
function op_simple($field) {
$value = intval(strtotime($this->value['value'], 0));
if (!empty($this->value['type']) && $this->value['type'] == 'offset') {
$this->query->add_where_expression($this->options['group'], "$field $this->operator DATE_ADD(NOW(), INTERVAL $value SECOND)");
} else {
$this->query->add_where_expression($this->options['group'], "$field $this->operator $value");
}
}
}
@niklasf
Copy link

niklasf commented Sep 9, 2011

Great, this is just what I was looking for.
Just one thing I still need to research: How do I provide filters for fields on entities?

@lsolesen
Copy link
Author

I don't know.

@niklasf
Copy link

niklasf commented Sep 16, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment