Slugs for Events section

Hi,

I am working on an events section on a site, and am wondering how to handle the slugs for the pages. For instance, there may be several called “Yoga Workshop”, but after the first with the slug yoga-workshop is created, adding another will trigger the “A page with the URL appendix “yoga-workshop” already exists” warning.

What is the most editor-friendly way of addressing this? I guess it must come up with news sections as well with titles like “Weekly Update” etc. Ideally I don’t want the editor to have to worry about this

I would think about a new strategy. Use more meaningful titles that are also optimized for SEO. The title “Yoga workshop” doesn’t say so much about the event. Add more information to the title:

  • Yoga workshop for beginners
  • Yoga workshops in December
  • Yoga workshop with Julia and Jane

Hi,

That is certainly an option, and I appreciate the SEO benefits, but I think given the potential lifetime of the site that the potential for identical event titles is high. I want to remove the issue of an editor having to try various options to find a slug that hasn’t been used previously.

I think something like using the event start date as part of the slug would be a good solution, but I don’t think that is possible even with the new page creation dialogue options.

Here is a similar post, but not with a code example, just a suggested solution:

One idea is to automatically change the slug as soon as the event has ended and the page has a different status. Adding a timestamp would be one way to avoid duplicate titles in the future.

status: listed: → yoga-workshop
status: unlisted: → archived20231211-yoga-workshop

What you could do is create a page model that automatically adds a date or unix timestamp after the title (overwriting the $page::create() method.

I wouldn’t use a hook for that, because when you change the slug after page creation (which happens in a hook), then you will not be automatically redirected to the newly created page, but editors will be shown an error that the page doesn’t exist.

That’s exactly what I mean. But with so many options, I didn’t recommend the best solution. Thank you @texnixe for the correction.

Hi @mikeharrison

I did exactly the thing you want to do.

create a file called event.php in site/models

paste in this code:

<?php

// Learned this from https://forum.getkirby.com/t/auto-append-to-url-appendix/19722
// on August 28, 2023.
// Found it googling 'kirby cms append number to slug'.
// Yay! Page models FTW!!!!!

// For a content file called 'event.txt'
// In general the class name is {{ShowFileName}}Page

    use Kirby\Cms\Page;
    use Kirby\Toolkit\Str;

    class EventPage extends Page
    {

        // all methods of the Page class are inherited and can be overridden here now.

        public static function create(array $props): Page
        {
            // $props['slug'] = Str::slug('Hello World'); ///// zu Testzwecken :)

            $page = new Page($props);
            if ( kirby()->page( $page->id() ) ) {
                 $props['slug'] = $props['slug'] . '-' . Str::random(16);
            }

            return parent::create($props);
        }

    }

I left my comments in there to remember like in a year or so … if I ever have to revise this … what I have done.

Check out the link in the comment (Auto-append to URL-Appendix)

What this does: It adds a random string of 16 characters to the URL appendix. That way it is very unlikely that two slugs are ever the same.

Hope this works for you.

If you need assistance in hiding the URL appendix in the panel (in case that’s something you want to do) let us know.

Thanks everyone this is really useful. I am going to set up a page model to handle this, and add a unix timestamp to ensure individuality.

If I were to add the event start date field to the Page Creation dialog, would it be possible to access this as part of the page model? As that would be ideal to add to the slug, rather than a random number or a timestamp

1 Like

Yes

Until there are two equal events on the same date :wink:
I think I still would add something truely unique - maybe the page uuid?

Then the user would still get an error message and could correct it. So dealing with this depends really on the probability that this might happen, i.e. two yoga workshops with the same title on the same startdate, rather unlikely and maybe even an error that should be surfaced?

You’re right, depends on the context.
Could totally be a feature :smile: