Created
April 1, 2011 08:56
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'), | |
); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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"); | |
} | |
} | |
} |
I don't know.
Figured that out now: https://github.com/niklasf/birthdays/blob/7.x-1.x/views/birthdays_field_views_handler_filter.inc
It's basically the same :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?