Perry
November 9, 2019, 3:38pm
1
I use the date field as id for an event calendar. If the user duplicates a page using the “id” of the source, how can I automatically set the timestamp at the time of the copy?
created:
label: created
type: date
time: true
width: 1/2
default: now
disabled: true
You probably need a plugin similar to the Custom Add Fields plugin but for the duplicate dialog. Other than that, you probably have to set the slug in a hook.
Perry
November 9, 2019, 4:54pm
3
Yes, that’s the one. And no, there is no tutorial, but there is not much to do. What you have to do inside the hook is call $duplicatePage->changeSlug()
.
Perry
November 9, 2019, 5:04pm
5
can I change the contents of /content/events/xy/.xy.txt with the hook?
Yes, of course, but you were talking about the ID.
Perry
November 9, 2019, 5:07pm
7
yes, but I use the timestamp as id, I won’t change the slug
Well, the slug is part of the page ID, but I guess we are talking at cross purposes.
Perry
November 9, 2019, 5:12pm
9
I only need a unique number, so I’m using the date field. Presumably, there is also a better solution.
At the end I create from the page content a json file to make it available to another website to record the event in his program.
<?php
header('Content-type: application/json; charset=utf-8');
$data = $pages->find('veranstaltungen')->children()->published()->filterBy('category', '!=', '')->flip();
foreach($data as $article):
//image
$image = $article->event_image()->toFile();
$thumbURL = $image? $image->resize(1000)->url(): '';
//date
$event_date_start = date("Y-m-d H:i",strtotime($article->date_event_start()));
$event_date_end = date("Y-m-d H:i",strtotime($article->date_event_end()));
//id
$event_id = strtotime($article->created());
$json[] =
[
'event_id' => (string)$event_id,
'event_title' => (string)$article->article_title(),
'event_description' => (string)$article->text_full(),
'image_url' => (string)$thumbURL,
'detail_url' => (string)$article->url(),
'event_categories' => [(string)$article->category()],
'event_dates' => [["start_date" => (string)$event_date_start,"end_date" => (string)$event_date_end]],
'venue_name'=> "",
'venue_address'=> "",
'venue_city' => 'Biel/Bienne',
'venue_zip' => '2502'
];
endforeach;
$json_parser = json_encode($json);
echo '{"api_key": "DFCS-AzU4k8kmJc", "events": '.$json_parser.'}';
?>
Perry
November 9, 2019, 7:41pm
10
I tried this:
'hooks' => [
'page.duplicate:after' => function ($duplicatePage) {
if($duplicatePage->template() == "news")
{
try {
$newcontent = $duplicatePage->update([
'title' => 'A new title'
]);
echo 'The page has been updated:';
} catch(Exception $e)
{
echo $e->getMessage();
}
}
}
]
error message: The JSON response from the API could not be parsed. Please check your API connection.
What this mean ?
Edit:
but the page is duplicated (after a reload)
texnixe
November 9, 2019, 10:45pm
11
The problem is your echo statement, you cannot return or echo anything from a hook, only throw an exception.
Perry
November 10, 2019, 9:18am
12
Okay thank you @texnixe I mixed https://getkirby.com/docs/reference/objects/page/update with the hook.
Now it works:
'hooks' => [
'page.duplicate:after' => function ($duplicatePage) {
if($duplicatePage->template() == "news")
{
try {
$dateNow = date_create()->format('Y-m-d H:i');
$newcontent = $duplicatePage->update([
'article_title' => 'new title',
'created' => $dateNow
]);
} catch(Exception $e)
{
$e->getMessage();
}
}
}
]