Auto-append to URL-Appendix

Hey…
i just created an events-subpage for my site.
Wen have multiple events with same title but different content. so we have to edit the url-appendix. is there a way of auto append a counting number or the event-date, when the autogenerated url already exists?
(i’m using this template as a start and i don’t know if this is a kirby or a template related question)

You could auto-create the slug in a page model, by overriding the create method, example: Create page slug from two fields

1 Like

thx a lot!
this helped me for a solution:

<?php
use Kirby\Cms\Page;
use Kirby\Toolkit\Str;

class EventPage extends Page
{

    public static function create(array $props): Page
    {
        $props['slug'] = Str::slug($props['content']['title'] . "-" . Str::random(3));
        
        return parent::create($props);
    }
}

just a “cosmetic question”: is it possible to also see this in the preview area while creating the page? currently the preview is generated live from title while entering title

No, because this happens when the page is actually created, not in the create page dialog. The dialog is client side, the model is server-side code.

Maybe you also want to add a condition to only execute this if the slug is already taken?

And instead of always creating the slug from the content title, you might want to use the slug the user created, so

 if ( page( $props['slug'] ) ) {
   $props['slug'] = $props['slug'] . "-" . Str::random(3);
}
1 Like

hey… thx a lot… that is something i wanted to build next =)
the condition seems not to match… i still get an “already exists” alert.

Ok, just checking for the slug was nonsense, you need the full path of course if the page is not on the first level.

 if ( page( $this->parent()->id() . '/' . $props['slug'] ) ) {
   $props['slug'] = $props['slug'] . '-' . Str::random(3);
}
1 Like

first: thx a lot!

maybe we get near… but there is a new alert Using $this when not in object context

Alright, forgot that its a static method. Then you probably have to hardcode the parent id:

 if ( page( 'events/' . $props['slug'] ) ) {
   $props['slug'] = $props['slug'] . '-' . Str::random(3);
}

(or whatever the path to the page)

The alternative would be to create a new page instance:

 public static function create(array $props): Page
    {
        $page = new Page($props);
        if ( page( $page->id() ) ) {
             $props['slug'] = $props['slug'] . '-' . Str::random(3);
        }
       
        
        return parent::create($props);
    }

Which would make sense if the parent id might change in the future.

1 Like

yes! seems to work with listed and unlisted pages… but not with drafts =)

Well, that’s for you to find out :wink:

Use kirby()->page() then

1 Like

you are gorgeous! :pray:

thx a lot…

so… final solution (adding random 3 digit) for anyone who searches… :smile:

site/models/event.php

<?php
use Kirby\Cms\Page;
use Kirby\Toolkit\Str;

class EventPage extends Page
{

 public static function create(array $props): Page
    {
        $page = new Page($props);
        if ( kirby()->page( $page->id() ) ) {
             $props['slug'] = $props['slug'] . '-' . Str::random(3);
        }
       
        
        return parent::create($props);
    }
}

where EventPage and filename of model depends on template name, like described here: https://getkirby.com/docs/guide/templates/page-models