Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Hi,
My goal is to display a 'datetime' (MySQL) field with Views module, but unfortunately I only get "1970/01/01 - 02:33" (however the date is stored correctly in the field itself).
As I read the documentation and the few related hits I found with Google, it seems that I need to create a hook which specifies how the fields should be handled. I made the followings:
First in hook_views_data:
// Implementing hook_views_data()
function hublist_views_data()
{
$data = array();
// [A lot of other fields here]
// [...]
// Latest ping date
$data['hublist']['stat_pingdate'] = array(
'title' => t('Latest ping attempt'),
'help' => t('Latest ping attempt'),
'field' => array(
'handler' => 'views_handler_field_date',
'click sortable' => TRUE,
),
'filter' => array(
'handler' => 'views_handler_filter_date',
),
'argument' => array(
'handler' => 'views_handler_argument_date',
),
'sort' => array(
'handler' => 'views_handler_sort_date',
),
);
Then I also have this hook in my .module file:
function hublist_date_api_fields($field) {
$values = array(
// The type of date: DATE_UNIX, DATE_ISO, DATE_DATETIME.
'sql_type' => DATE_DATETIME,
// Timezone handling options: 'none', 'site', 'date', 'utc'.
'tz_handling' => 'site',
// Needed only for dates that use 'date' tz_handling.
'timezone_field' => '',
// Needed only for dates that use 'date' tz_handling.
'offset_field' => '',
// Array of "table.field" values for related fields that should be
// loaded automatically in the Views SQL.
'related_fields' => array(),
// Granularity of this date field's db data.
'granularity' => array('year', 'month', 'day', 'hour', 'minute', 'second'),
);
switch ($field) {
case 'hublist.stat_pingdate':
return $values;
}
// The following return may ensure that it return the values without any condition...
// Just for the case I made the previous switch wrong...
return $values;
}
Could someone help me what do I wrong?
Comments
Comment #1
fleetcommand CreditAttribution: fleetcommand commentedConverted to a bug report. It seems the hook_date_api_fields is not fired at all...
Added the following line to the beginning of my hook, but for some reason it does not appear in the database log which shows that this hook is not fired at all. Updated today to the -dev version to ensure that the bug still present.
Comment #2
dropcube CreditAttribution: dropcube commentedCheck this issue: #479996: views_handler_field_date for custom module outputs as if default UNIX date type (everything's 1969)
Comment #3
chiddicks CreditAttribution: chiddicks commentedOf note is that in addition to this hook (hook_date_api_fields) you need to declare your tables using hook_date_api_tables(). If you implement the former and not the latter your hook will be called for other fields but not your own.
Comment #4
ARUN AK CreditAttribution: ARUN AK commented@chiddicks, could you please provide any example code of hook_date_api_tables?