Jump to:
| Project: | Event Views |
| Version: | 6.x-2.x-dev |
| Component: | Code |
| Category: | bug report |
| Priority: | critical |
| Assigned: | Unassigned |
| Status: | closed (fixed) |
Issue Summary
Hi,
I have spent about 6 hours yesterday working through getting Event Start Date into a view in drupal 6.13 and I would like to share my experences.
The Views 2.x API is here: http://views.doc.logrus.com/
This lists all the hooks and it is clear from this that hook_views_data must be implemented to add fields, etc. to the views module.
I have downloaded the event views module (6.x-2.x-dev) and this does not implement this hook. Instead it implements a hook called hook_views_tables which dosn't even exist in version 2.x API. My guess is that it is still written for a previous version of the views API. Despite this it reports itself as a drupal 6 module. Since drupal 6 must use views 2.x API it's basically faking it and I don't see how it has ever worked.
My solution for this is something that worked for me and people can use it as a start. (Although not a professional quality fix)
Important things to note if you are following this to do a views 2.x API
- Some views hooks are in the module file, other views hooks are in special inc files. Watch which goes where
- You must include hook_views_api () in your module file
- You have to use the right file names for the inc file
- You must goto Adminster -> Site Building -> Views -> Tools and press the Clear Views Cache button to see any of this
I devloped a site custom module called xxx_custom and created the following directory:
/sites/all/modules/xxx_custom
Create a file called xxx_custom.info and fill in the following
; $Id$
name = xxx_custom
description = Custom module for GKR15 Website
version = "0.0.0.2"
core = "6.x"Create a file called xxx_custom.module and fill in the following
<?PHP
/**
* Implementation of hook_views_api().
*/
function xxx_custom_views_api() {
return array(
'api' => 2,
);
}Create a file called xxx_custom.views.inc and fill it with:
<?PHP
/**
* Implementation of hook_views_data().
* Expose event fields and filters to the views module.
*/
function xxx_custom_views_data() {
$data['event']['table']['group'] = t('Event (RJM)');
$data['event']['table']['join'] = array(
'node' => array(
'left_field' => 'nid',
'field' => 'nid',
),
);
// Node ID field.
$data['event']['nid'] = array(
'title' => t('Event Data'),
'help' => t('Event data linked to the node'),
'relationship' => array(
'base' => 'node',
'field' => 'nid',
'handler' => 'views_handler_relationship',
'label' => t('Event Node'),
),
);
// Example numeric text field.
$data['event']['timezone'] = array(
'title' => t('timezone'),
'help' => t('Just a numeric field.'),
'field' => array(
'handler' => 'views_handler_field_numeric',
'click sortable' => TRUE,
),
'filter' => array(
'handler' => 'views_handler_filter_numeric',
),
'sort' => array(
'handler' => 'views_handler_sort',
),
);
// Example timestamp field.
$data['event']['event_start'] = array(
'title' => t('Event Start'),
'help' => t('Event Start Date and Time'),
'field' => array(
'handler' => 'views_handler_field_rjm_date',
'click sortable' => TRUE,
),
'sort' => array(
'handler' => 'views_handler_sort_date',
),
'filter' => array(
'handler' => 'views_handler_filter_date',
),
);
return $data;
}
?>Finally create a file called views_handler_field_rjm_date.inc and fill it with:
<?PHP
class views_handler_field_rjm_date extends views_handler_field_date {
function render($values) {
$value = $values->{$this->field_alias};
$format = $this->options['date_format'];
if (in_array($format, array('custom', 'raw time ago', 'time ago', 'raw time span', 'time span'))) {
$custom_format = $this->options['custom_date_format'];
}
//our format 2008-03-30 10:00:00
$old_value = $value;
//$value = date("Y-m-d H:i:s",$old_value);
$hr = (int) substr($old_value,11,2);
$min = (int) substr($old_value,14,2);
$sec = (int) substr($old_value,17,2);
$day = (int) substr($old_value,8,2);
$mon = (int) substr($old_value,5,2);
$year = (int) substr($old_value,0,4);
$value = mktime($hr,$min,$sec,$mon,$day,$year, -1);
//$custom_format = "Y-m-d H:i:s";
if (!$value) {
return theme('views_nodate');
} else {
$time_diff = time() - $value; // will be positive for a datetime in the past (ago), and negative for a datetime in the future (hence)
switch ($format) {
case 'raw time ago':
return format_interval($time_diff, is_numeric($custom_format) ? $custom_format : 2);
case 'time ago':
return t('%time ago', array('%time' => format_interval($time_diff, is_numeric($custom_format) ? $custom_format : 2)));
case 'raw time span':
return ($time_diff < 0 ? '-' : '') . format_interval(abs($time_diff), is_numeric($custom_format) ? $custom_format : 2);
case 'time span':
return t(($time_diff < 0 ? '%time hence' : '%time ago'), array('%time' => format_interval(abs($time_diff), is_numeric($custom_format) ? $custom_format : 2)));
case 'custom':
return date($custom_format, $value); //format_date($value, $format, $custom_format);
default:
return date($format, $value);
}
}
}
}
?>Now goto Adminster -> Site Building -> Views -> Tools and press the Clear Views Cache. When you create a view you will now see the event start field. You should use this with a custom format and put in a PHP style format string to view the date.
As you can see I have used the API documentation example to produce the result. When this worked I found that the default date handler didn't work. I have had to create my own but I don't understand how Drupal handles dates so I have written the code in a way that works for me, but I think there is a proper way to do it which isn't this way!
Hopefully this post will stop others having to spend 6 hours figuring this out.
Comments
#1
Two things I forgot to mention
Firstly, this won't work until you activate the custom module.
The second thing is I have only put in the event start date code because that was all I needed.
To get event end date you need to adjsut xxx_custom.views.inc
and add something like
// Example timestamp field.$data['event']['event_end'] = array(
'title' => t('Event End'),
'help' => t('Event End Date and Time'),
'field' => array(
'handler' => 'views_handler_field_rjm_date',
'click sortable' => TRUE,
),
'sort' => array(
'handler' => 'views_handler_sort_date',
),
'filter' => array(
'handler' => 'views_handler_filter_date',
),
This is untested
Add this after where the event start code is. (It's just copied and pasted) Don't forget to clear the views cache or you won't see it.
I don't know if there are other event fields that could be added because I don't use the events module (doing this to help out a friend on his site.)
You should add these by looking at the example code in views 2.0 api for the spercific type and putting it into xxx_custom.views.inc
#4
Thank you)))
#5
When I use the above i get:
Error: handler for event > event_start doesn't exist!
After I try to add the start date field in my view.
Aren't handlers supposed to be declared ?
(Asks the newbie)
#7
I have cobbled together the work in this thread and created a working module that replaces the event_view module since it does not support Views API 2 at all and has not done so for some time now. You can download it here:
*link removed*
It supports ALL of the fields in the Event table for View Filters, View Sort, and View Field. The work done in this thread was close to being completed, it just needed some polishing off and to be put into module form.
Simply DL and enable this module and disable the existing event_views module as its no longer needed. I will contact the author of this module to find out if we should split the projects or combine them since I have my own cvs account, I could just toss it up as a new project, but I'll speak with them first.
EDIT: I've uploaded a new snapshot for this project. The link I had previously posted is no longer necessary.
#8
I've uploaded a new development snapshot that closes all of the drupal 6 / views API 2 problems. Closing all issues related to this problem.
#9
Hi Crystaldawn,
Thanks for putting this in the actual module.
I will contact my friend who is using this and I'll let him know he can deactavate the fix I created and replace it with the proper event views module as it will work now.
Robert
#10
Remember when you DL the new release, unzip it, disable it, UNINSTALL IT (this is super important, dont miss this step), and then re-enable it and you'll be all set to go.