Autofill field in panel like the URL appendix on page creation?

So I’ve been tinkering with this idea on and off today and I’m wondering what would be the best way to achieve this.

Let’s say I have this (blueprint):

sectionTitle:
  label: Section title
  type: text
sectionID:
  label: Section ID
  type: text

This I can then use in a snippet or template like this:

id="<?php echo $page->sectionID()->html() ?>"
href="#<?php echo $page->sectionID()->html() ?>"

So to my question. When creating a new page, we have the URL appendix field which get auto filled and turns the URL friendly format, such as my-awesome-page-url. Is there an easy way to achieve this in a blueprint, in some way like the example above?

You could use a panel hook that automatically ads the uid to the content file when the page is created/updated.

I’ve never used the panel hooks, only read about it briefly.

I’m guessing you suggest using panel.page.create (and maybe even panel.page.update?) to add/update the UID in the field?

Is there a “proper” field type for this? I guess I could use the hidden one, but I would still like it to be editable in case the title of the page goes out of control, so you can still keep the “hashed” URL somewhat pretty. :slight_smile:

I would use a field of type text. As for using the hook, that is quite simple:

kirby()->hook('panel.page.create', function($page) {
        $uid = $page->uid();
        try {
            $page->update(array(
                'uid' => $uid
            ));
        } catch(Exception $e) {
            echo $e->getMessage();
        }
    
});

and then do the same for panel.page.update(), in case the uid changes.

Once the page is created, the UID of the panel folder does not change anymore if the title is changed.

1 Like

I’d only use it with the creation hook, otherwise you won’t be able to manually change the value (it will be overridden every time).

That’s true, I just wonder what the purpose of the field is supposed to be? Does it need to be changed if the uid of the field is changed or not? Or should it be static?

If you only need to have a “URL safe” name and don’t need to change it, you can simply use $page->uid() without any blueprint fields or hooks.

Yes, now that I think it through a bit more, it makes sense to only have on the panel.page.create.

Just as long as you are able to change it, if needed of course. Just using $page->uid() for this kind of thing would definitely work, however in my particular case it’s not quite cutting it.

In all honesty, I might be quite anal about this, but having something like my-url.com/whatever#this-is-the-original-title-for-the-section-and-is-quite-long is “unfortunately” a possibility. :slight_smile: So yes, the UID only could (and would) most definately work in most cases, however, in this instance I want to save myself some trouble and actually have it editable.

If I’ve understood correctly, you want to display several subpages as sections on a single page. If you don’t need a separate url you can simply change the url after page creation.