Automatically generate url appendixes

This has been asked before (kind of) but I believe for different scenarios.

In this website, there is a ‘fairs’ page that has ‘fair’ children. Many of the real world fairs that will go here happen annually, and they always have the same name, for example ‘Arco’.

So when the user adds a new fair, she gives it the name ‘Arco’. But a fair with the same name and url appendix exists already from the previous year.

She can manually alter the url appendix for example adding the year ‘arco-2023’, but my question is if this can be done automatically for that particular kind of pages.

Additionally I’d like to know if the url appendix field from the create page dialog can be blocked to changes OR hidden, and still autocomplete and function as usual.

Thanks

Yes, you can achieve this via a page model, in which you overwrite the create method and thus modify the slug automatically.

As regards your second question, there is a plugin that allows you to change the page create dialog per model/blueprint: Custom Add Fields | Kirby CMS

Great. thank you

But but… hmm… let’s say I do not use the custom add fields plugin, but only attempt to overwrite the create method in a custom page model.

When I click ‘create draft’, where does the validation of the slug happens? I mean the validation that ensures that there is no duplicate and, when there is, shows a red label on top of the create page.

Is this validation happening in the create method of the original page class?

/edit

I see that the validation happens in the changeSlug method of PageRules.php, but I don’t understand when this method is invoked in relation to the create method as defined either in the Page class or in PageActions.php

edit/

I am asking to understand if the custom create method would be reached at all, if I let the regular slug field in place on the create dialog. Or if the validation wpuld kick in before reaching the custom create method.

Thanks

You can do this check inside the create method, e.g.

public static function create(array $props): Page
{
    if (kirby()->page('fairs/' . $props['slug'])) {
        $props['slug'] = Str::slug($props['content']['title'] . "-" . date('Y'));
    }

    return parent::create($props);
}

so check if a page with the given slug already exists, if not, modify it by appending the current year.

Of course, this will then fail if a page with the slug and the current year attached already exists, but that shouldn’t be the case if these events are only created once per year.

Thanks @texnixe

I am a bit confused regarding page models.

If we overwrite the page create() method in a model, the method we are overwriting is this one?

Also, are we effectively 100% bypassing the original method and substituting it with what we code here?

If that is correct, what happens with the rest of the code in the original method, such as (if that is the rigth method) producing the uuid, running the hooks etc?

This is probably unnecessary to achieve what I need, as you already provided the code, thank you, but I’d like to understand if possible

Thanks

Well, if you look at the code above closely, after modifying the slug, we call the parent method.

So yes, we overwrite the parent method in the child class, but modify only a bit, then continue with the parent method.

aaah I see now :))

…and since $props['slug'] is already defined, nothing else is done to it.

One last question, please, when/how is the duplication validation method invoked in the process of creating a Page? (which I believe is in PageRules.php)

Thanks

Exactly.

If you follow the parent create method into the commit method, you will see $this->rules()->$action(...$argumentValues); Which then calls the corresponding rules, in the case the PageRules::create method.

You should be able to follow these methods by CMD-clicking on them… Or you use a step debugger and set breakpoints: First steps to start debugging with Xdebug | Kirby CMS

I already convinced someone else :wink: Fileupload over routes - #16 by Microman

1 Like