Use date as title in panel

I’ve updated a website from Kirby 2 to 3. On a page template I use a date field that automatically alters the title of the page in the date.

In Kirby 2 I used a plugin for that, but in Kirby 3 this is not working. So I wrote it als a hook in the config file. But somehow I don’t get this to work.

What am I doing wrong? Or beter, is there another way in Kirby 3 to accomplish the goal (have a date field als title.

  'page.update:after' => function ($page) {
        
  			if ($page->intendedTemplate() === 'event') {
  			
    			$title = $page->date()->toDate('Ymd');
    			$uri = str::slug($title);
    			// Stop if there's a sibling with same date
    			if (! $page->parent()->find($uri)) {
      				// otherwise update title and URI
      				$page->update(compact('title'));
      				$page->move($uri);
    				}
  			}
  				
  				

        }

The hook will always result in an error, because you are not redirected to the new page.

I’d recommend the Custom Add Fields plugin:

With this plugin, you can even skip the title completely, I think.

Ok. But the idea is that if the date is changed later on, then the title is updated as well.

Can the title field be an date field?

Oh, ok, but that means that the URLs to these pages will keep changing as well whenever a user changes the date? Just wondering if that is a good idea… What is your use case? And why does the date have to be in the title/slug at all? What happens if there are multiple events with the same date?

But as I wrote above, when you change the slug in a hook, you will not be redirected to the new location and your users will see an error.

True. That might not be a good idea, as you explained.

The reason that I set it up like this, is that the page order is always by date. It’s an event calendar. So it’s logical to have a chronicle order of te events in the panel and content folder. But maybe this can be achieved in a different way.

Thank you for your feedback!

You can sort your pages by date in a pages section, so no need to have the title.

In the frontend, it’s not an issue anyway.

A consistent URL will also be better for SEO reasons, otherwise, you would have to keep track of them all the time and use redirects.