Is there a way to unpublish events automatically after their date?
andrewfn - August 5, 2006 - 02:36
I know about the schedule module, but that requires extra information to be entered on every node. Event nodes already have dates. It would seem a very common requirement to have events disappear once they are past. Does anyone know of a way to accomplish this?

I'm pretty sure that
I'm pretty sure that http://drupal.org/project/scheduler will do what you want.
www.aussiecarpool.com
share the cost of commuting
www.justapickle.com
blogging community for the socially conscious
www.slickfish.com.au
professional, affordable web site design, production and maintenance for small business
Unpublish from where? From
Unpublish from where? From the page? They disappear automatically from the Upcoming events block once the time is past.
unpublish from: taxonomy related pages
Several of my sites have a lot of events. The way I structure the sites is to tag all the events with a taxonomy name and then all events of that type can be viewed from associated the taxonomy page, which I link to a menu item. Everything works very well, except that old events have to be manually unpublished or they still keep appearing.
Solved the problem
I have solved the problem by writing the following ultra-simple module called eventhide. If an event is more than a day old, then it's 'changed' date is set to 1999-12-31. I have replaced the standard taxonomy view using the views module to exclude nodes that have changed dates before 2000-01-01 and to put the items in ascending order. Stickies will still show at the top. The advantage of this approach is that if an event gets modified so that it is in the future again (e.g. by recycling old events), then the 'changed' date will automatically be updated and the event will be unhidden.
The code shows an alternative line of code which will unpublish events, but then of course they will disappear from the calendar.
I would value any comments on the code:
<?php
// $Id: eventhide.module,v 1.0.0.0 2006/08/06 17:30:14 Andrewfn Exp $
/* If an event is more than a day old, then it's 'changed' date is set to 1999-12-31.
* To use this, replaced the standard taxonomy view using the views module to exclude nodes that have changed dates before 2000-01-01
* and to put the items in ascending order. Stickies should still show at the top.
* The advantage of this approach is that if an event gets modified so that it is in the future again (e.g. by recycling old events),
* then the 'changed' date will automatically be updated and the event will be unhidden.
* The code shows an alternative line of code which will unpublish events, but then of course they will disappear from the calendar.
*/
/**
* Implementation of hook_help().
*/
function eventhide_help($section) {
if ($section == 'admin/modules#description') {
return t('A module to hide events that are out of date by setting [changed] to before 2000 so they can be filtered out by the views module.');
}
}
/**
* Implementation of hook_perm().
*/
function eventhide_perm() {
return array('mark event nodes that are out of date');
}
/**
* Implementation of hook_cron().
*/
function eventhide_cron() {
$clear_cache = FALSE;
//if the event_end time is more than a day old, set the change date to 1999-12-31 so that the query module can hide it
$nodes = db_query('SELECT * FROM {node} n LEFT JOIN {event} e ON e.nid = n.nid WHERE event_end < %d -86400 AND changed > 946600000', time());
//alternative line of code to unpublish the node (to use, comment previous line and uncomment following line)
//$nodes = db_query('SELECT * FROM {node} n LEFT JOIN {event} e ON e.nid = n.nid WHERE event_end < %d -86400 AND n.status = 1', time());
while ($node = db_fetch_object($nodes)) {
//if this node is to be hidden, we can update the node
db_query('UPDATE {node} SET changed=946600000, promote=0 WHERE nid = %d', $node->nid);
//alternative line of code to unpublish the node (to use, comment previous line and uncomment following line)
//db_query('UPDATE {node} SET status=0 WHERE nid = %d', $node->nid);
watchdog('content', t('%type: scheduled hiding of %title.', array('%type' => theme('placeholder', t($node->type)), '%title' => theme('placeholder', $node->title))), WATCHDOG_NOTICE, l(t('view'), 'node/'. $node->nid));
$clear_cache = TRUE;
}
if ($clear_cache) {
// clear the cache so an anonymous poster can see the node being published or unpublished
cache_clear_all();
}
}
?>
Well, I tried this, but now
Well, I tried this, but now all my dates are out of my calender...
Only odd saterdays and odd sundays show up now, and dates with events.
I removed your module and everything is still blanc in IE6. In Firefox, all dates are being displayed again normally. Don't know if this is an IE problem, cache problem, or problem with your code.
IE problem / cache
The only thing my code will modify is the "changed" date on each node. It runs a check and makes changes every time cron is run. If you remove the module, it won't put the old values back again since the change is made in the database.
Unless you set up a view that makes use of this new changed date, then you will see absolutely no difference on your site. The fact that dates are being displayed in Firefox means that your pages are being generated correctly by Drupal. I strongly suggest that you turn off caching while developing the site otherwise you *will* get strange effects. Also it is a good idea to close and restart IE to force it to refresh.
thank you
this seems to working well for me,
I have chosen to leave the nodes published for the sake of non-broken (and recycling ;)) URL's and I have also set up an admin "view" to show which nodes I can then recycle more easily ~ I left the comment count showing so I don't recycle events which perhaps had comments
I don't know if this is the best way but I like it, thank you for your ideas and code!
event_cron method
I added this method to event module, to remove the stickies (on top of page) and promote (on frontpage) of the event:
/**
* Implementation of hook_cron().
* @ingroup event_core
*/
function event_cron() {
$curtime = time();
//calculate the closing time for the event
//when closing is preferred. query the database for all events which have a end time less than this,
$closing_time = $curtime ;
$result = db_query("SELECT nid FROM {event} WHERE event_end < %d", $closing_time);
//loop through the results, calling and remove the sticky for the event
while ($event = db_fetch_object($result)) {
$node = node_load($event->nid);
if ($node->sticky == 1) {
db_query("UPDATE {node} SET sticky = 0 WHERE nid = %d", $event->nid);
watchdog('event', t('Sticky removed for %event by cron.', array('%event' => $node->title)));
}
if ($node->promote == 1) {
db_query("UPDATE {node} SET promote = 0 WHERE nid = %d", $event->nid);
watchdog('event', t('Promote removed for %event by cron.', array('%event' => $node->title)));
}
}
}
That was enough for me. I didn't want to delete the old events.
Might help some.
Marc
Yes, this worked for me,
Yes, this worked for me, too.
I changed the code a bit to unpublish expired events, by changing the 'sticky' variable to 'status'.
if ($node->status == 1) {db_query("UPDATE {node} SET status = 0 WHERE nid = %d", $event->nid);
watchdog('event', t('Expired %event unpublished by cron.', array('%event' => $node->title)));
}
Thank you for this code,
Thank you for this code, worked great with Drupal 5.x
Great
I will try these codes they are exactly what I want.
Richard Ma
www.meetcampus.com