Slug = Incremental Number

Hey all!

I’m using Kirby for a small library catalog. Each book has its own number which is used as the slug for the respective pages. This works perfectly for all the books that are already in the catalog. When I add a new book (= create a new page), I’d like Kirby to generate its number (latest/highest number + 1), fill in the number field and also make it the page’s slug. Is that possible?

Many thanks,

Yes, you can do that via a Page model by overriding the writeContent() method. Ideally, you would do this in conjunction with the Custom add fields plugin, so that you can remove the slug field from the “Create page” dialog.

Thanks for your swift reply! I installed the Custom add fields plugin, but I think I still need some help setting up the Page model. I’ve got this basic structure in book.php:


class BookPage extends Book
  public function slug() {
    return xxx

So what I would need to do is make it look up the most recent number and then override the default slug with this number +1, right? I’m not sure as to how this should be done.

No, as I said above, you need to override the writeContent() method and inside it, set the slug.

And you, you need to get the last number.