If you're upgrading your site from Drupal 5 to Drupal 6 and were previously using the event module, you have probably realized that a lot of the supporting modules that made the event module functional (such as Event Views) have fallen by the wayside. Well, as luck would have it, much (if not all) of the functionality that the event module provides can be re-created using CCK date fields, views and the calendar module. So the only task that remains is to get your existing event content migrated over into usable CCK fields, and then configure your views to display them into a calendar display.
While there are undoubtedly other approaches to achieve this end, this is a relatively quick and painless path to get the features you want up and running.
You'll require the following D6 supporting modules:
CCK - http://drupal.org/project/cck
Date - http://drupal.org/project/date
Views - http://drupal.org/project/views
Calendar - http://drupal.org/project/calendar
Install and enable the latest revisions of these modules.
Identify the content type or types currently on your site which are used by events. The process differs slightly depending on whether the resulting CCK date fields will be located in only one content type, or shared between multiple types.
Through the CCK interface, create new date fields in your existing event content type. These are the fields into which the old event information will be migrated . For the purposes of this document, I'll call them "eventstart" and "eventend".
If you have only one content type for events, your new fields will be created in the mysql table corresponding to that type. For example, if your content type was called wine_tastings, a table called content_type_wine_tastings will exist in your database, and the fields field_eventstart_value and field_eventend_value will exist in this table.
If you're sharing the eventstart and eventend fields between multiple content types, new tables will be created to house this data. In this example, the tables would be called content_field_eventstart and content_field_eventend.
Now with a little php and mysql magic, we can populate the new field data from the event_start and event_end fields from the original event table. Because the date information is stored in slightly different formats, a little bit of transformation is required. Create a php script like the example below, and execute from the command line. This example was constructed using shared date fields across multiple content types.
Remember to ALWAYS back up your database before performing any operations like this!
<?php
// You backed up your database, right?
// Set up the mysql connection
// Drupal's bootstrap function may be substituted here
// require_once '/path/to/drupal/includes/bootstrap.inc';
// drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
$con = mysql_connect('localhost', 'yourdbusername', 'yourdbpassword');
mysql_select_db('yourdbname', $con);
// Cycle through all the records in the event table
$q = 'select * from event';
$r = mysql_query($q);
while ($row = mysql_fetch_assoc($r)){
// Transform the date information
// CCK date format: 2008-12-10T00:00:00
// event date format: 2006-11-13 15:00:00
$startdate = preg_replace ("/ /", 'T', $row['event_start']);
// Populate the new field for eventstart
$q2 = 'update content_field_eventstart set field_eventstart_value="'. $startdate . '" WHERE nid="'. $row['nid'] . '"';
if (!$r2 = mysql_query($q2)){
// Don't continue if anything goes south
exit ($q2 ." failed!\n");
}
// Perform the same transformation and data population for the end date
$enddate = preg_replace("/ /", 'T', $row['event_end']);
$q3 = 'update content_field_eventend set field_eventend_value="'. $enddate .'" WHERE nid="'. $row['nid'] .'"';
// Once again, terminate the operation if the query fails
if (!$r3 = mysql_query($q3)){
exit($q3 ." failed!\n");
}
}
?>Once you've got these fields populated, you should check an existing node to verify that the information has been correctly populated. Navigate to one of your event nodes, and select edit. You should see both the legacy event date information and the new CCK fields populated with the start and end dates.
If this all checks out, browse to the modules page and disable the events module. You are now free to uninstall the event module and purge any vestigial tables.
Congratulations! You have converted your event nodes to a more flexible CCK/Date setup. These fields are now available for use in views using the calendar module!
- Tags: Views, upgrade, Event Module, Drupal, Date Module, Date, convert, code, CCK, Calendar Module








Comments
Gerard writes:
Interesting post. I've been an event module user in the past, and based on your post, I'm wondering if it's perhaps best to migrate away from Event.module now?
It sounds like a much more flexible method of doing things - so thanks, as always for sharing!
venkat-rk writes:
Although I haven't tried it, this tutorial sounds very useful. Thank you for taking the time to write this.
Mark writes:
Thanks for the tips! It just so happened that I needed to do exactly this, except using event data from version 4.7. I adapted the script slightly:
<?phpwhile ($row = mysql_fetch_assoc($r)){
// Transform the date information// CCK date format: 2008-12-10T00:00:00
// event date format: 2006-11-13 15:00:00
// $startdate = preg_replace ("/ /", 'T', $row['event_start']);
$startdate = date('Y-m-d\TH:i:s', $row['event_start']);$enddate = date('Y-m-d\TH:i:s', $row['event_end']);
$nid = $vid = $row['nid'];
$q2 = 'INSERT INTO content_type_event (vid, nid, field_event_start_value, field_event_end_value) ' ."VALUES ($nid, $vid, \"$startdate\", \"$enddate\")";
// Populate the new field for eventstartif (!$r2 = mysql_query($q2)){
print ($q2 ." failed!\n");
exit;
}
}
?>Notably, after creating the CCK start and end date fields, there's no data in them - so I don't quite understand how your script populates the table using UPDATE sql statements? I use INSERT statements.
Also, the old event table didn't store revision info, but luckily revisions are disabled for my event nodes, so I could happily use the nid for both nid and vid fields.
The old event data was also stored as Unix timestamps, so I had to convert those. Seemed to work nicely!
Cheers
Mark
Dylan Tack writes:
On my site, the event module data was stored in the local timezone, while date uses UTC. As a result it was necessary to use the Date API to convert to the UTC timezone. I was also running this inside a hook_update_N(), so I used update_sql to perform the queries.
<?php
$events = db_query('select * from {event}');
while($event = db_fetch_array($events)) {
$startdate = date_make_date($event['event_start'], "America/Los Angeles", DATE_ISO);
$enddate = date_make_date($event['event_end'], "America/Los Angeles", DATE_ISO);
date_timezone_set($startdate, timezone_open('UTC'));
date_timezone_set($enddate, timezone_open('UTC'));
$startdate = date_format($startdate, 'Y-m-d\TH:i');
$enddate = date_format($enddate, 'Y-m-d\TH:i');
$results[] = update_sql("update {content_type_trainings} set field_event_date_value = '" . $startdate . "', field_event_date_value2 = '" . $enddate . "' where nid = '" . $event['nid'] . "'");
}
?>
Post new comment