Cancel adding a Page in PageModel - hookPageCreate

Hey Kirby Community,

I am working on a student page for my university.

It should be possible to add a student and change the Add Dialog. I used the Custom add fields plugin (thanks for that).

The process of adding a student by name works great and the detection of existing url slugs works.

But in the Add Dialog (if the name exists) a message appears that the given url already exists. Thats the expected behaviour but the page ist created with a timestamp (Custom Add Fields behaviour).

Is there a possibility to prevent page creation if the url exists in the hookPageCreate?

My Page Model:

class StudentPage extends Page {
public static function hookPageCreate($page){

    $vorname = $page->vorname();
    $nachname = $page->nachname();


    $page->update(array(
        'title' => $vorname . ' ' . $nachname,
        'vorname' => $vorname,
        'nachname' => $nachname
    ));


    $page->changeSlug(Str::slug($vorname . '-' . $nachname));
}

}

Hm, ok, looks like the page is first created and then renamed with the information from the model in a page.create:after hook. So in this case, you can’t prevent page creation. Hm, maybe this can be handled by the plugin, @steirico?

Is it possible to call $page->delete() in a page.create:after hook?

I tried it based on the generated slug in the StudentPage Model but I did not get it working.

Yes, you could delete the page in a hook, however, the slug is the timestamp and you would have to fetch that from the $oldpage parameter.

Okay Sorry, but I’m still working on a solution.

I want to avoid the error message in the add dialog.

My question is: When is the check done if the slug for creating a page exists.

Is it possible to run a hook / function before the error message is triggered.

I already tried to modify the parent page create method and the page is saved with a suffixed slug, but the error message in the add dialog remains.

Any ideas or hints how to solve this?

Sorry for the late reply.

According to my tests the problem can be solved in hookPageCreate as follow:

    try {
      $page->changeSlug(Str::slug($vorname . '-' . $nachname));
    } catch (Kirby\Exception\DuplicateException $e) {
      $page->delete(true);
    }

Hey,

thank you for that solution. Didn’t try jet but chatching the error makes sense :slight_smile:.

I ran into another issue that makes the plugin not useable for me.

My Setup is kirby version 3.1.0 and the latest Custom add fields release.

The dialog created by the Plugin does not validate required fields anymore.

I checked the index.js file and the latest commits on github but i could not determine why the validation process is not triggered. So pages are added even if (required) fields are emtpy.

The default Add Dialog of kirby works as expected.

Weird issue I am no able to solve yet.

1 Like