Once again I’m finding some time to work on this, and I actually made a sort of breakthrough! But there seems to be some misunderstanding as to what I’m trying to accomplish. So:
I would like to automatically set the unpublish_date
in the event’s header from its start
date. This way, past events will fall out of page collections automatically and I don’t have to do any extra date checking in the templates (which doesn’t go well with a limit set on a page collection).
Weirdly enough, what you are seeing here @anon76427325 about all the header dates being set to 0? I don’t get that at all. Whenever I put a timestamp anywhere, I get a Crikey error just trying to display the page…
But here’s a funny thing, I got it to work, in a way – I just think it’s probably very likely to break if I leave it like that. Here’s the code:
$header_old = $e->header();
// check if header.start is timestamp (int) or time string
$start = $header_old->start;
if (is_int($start)) {
$start = date("d-m-Y", $start);
} else {
$tmp = strtotime($start);
$start = date("d-m-Y", $tmp);
}
$time = " 0:00";
if (!empty($this->grav['config']->plugins['simple-events']['unpublish_time'])) {
$time = " ".$this->grav['config']->plugins['simple-events']['unpublish_time'];
}
$datetime = $start.$time;
$e->unpublishDate($datetime);
dump($e->unpublishDate());
dump($datetime);
$e->modifyHeader('unpublish_date', $datetime);
$e->save();
To recap: I’m getting the start date from the event page’s header, formatting it as a string, then I tack on the hours and minutes because unpublish_date
is a datetime.
Now if I use this variable $datetime
(a string) to set the unpublish_date
with the page’s built-in method, and then dump($e->unpublishDate());
, the output is a timestamp. If I save the page at this point, nothing appears to be saved or changed at all in the markdown file.
However, if I set the unpublish_date
via the modifyHeader
method… the correct datetime string gets saved, and all is well.
What is remarkable about this is that the string HAS to be in “d-m-y” format – “Y-m-d”, like my event start strings, does not work. I noticed that Grav would save any unpublish_date
that I added to an event via Admin in this format (plus hours and minutes). I’m pretty sure though that this may well be different depending on people’s setups, or Grav versions, or possibly language settings, …
I have found the source code for the unpublishDate method in system/src/Grav/Common/Page/Page.php
, and it definitely turns whatever it is handed into a timestamp. I haven’t been able to find the bit where the Admin plugin saves it as a datetime string though.
So I guess my question now is: What is the correct way to turn the unpublish_date in the header into the string format needed? Or how do I find out what that format is? (And out of curiosity: why does the timestamp set with the unpublishDate method not get saved?!)
Once again I am very grateful for any and all ideas!