In my website I can have several posts with the exact same title, so I’ve been using a date field to differentiate them in the slug, so I don’t get the error that the slug already exists.
I do this from the page creation modal:
create:
slug: "{{ page.title }}-{{ page.date.toDate('Y-m-d') }}"
fields: date
And my date field:
date:
type: date
default: now
display: DD/MM/YYYY
The issue is that if the date gets updated afterwards to use a different value (from the past, for example) the slug remains the same than when the page was created.
Is there any way I could sync the date field and the slug automatically if I change the date value?
Ultimately, I would even prefer that the slug and the folder of this page were exactly the same. When using this, the page folder does indeed update to reflect the change, unlike the slug:
num: '{{ page.date.toDate("Ymd") }}'
I have been reading the documentation and couldn’t find anything in this regard.
I’m sorry about this. For some reason I pasted the wrong code above. I did try with the example from the documentation, and got the same error message.
This is how my whole config.php looks like:
<?php
return [
'hooks' => [
'page.update:after' => function (Kirby\Cms\Page $newPage, Kirby\Cms\Page $oldPage) {
// your code goes here
}
]
];
You are absolutely right. I ended up solving my (slightly different) issue in the page.changeTitle:after hook. I removed my code snippet, as it would not work!